types.rs 9.79 KB
Newer Older
qinsoon's avatar
qinsoon committed
1
use ast::ptr::P;
qinsoon's avatar
qinsoon committed
2
use ast::ir::*;
3
use utils::vec_utils::as_str as vector_as_str;
4 5

use std::fmt;
qinsoon's avatar
qinsoon committed
6 7
use std::collections::HashMap;
use std::sync::RwLock;
qinsoon's avatar
qinsoon committed
8

qinsoon's avatar
qinsoon committed
9
#[derive(PartialEq, Debug, RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
10
pub struct MuType {
11
    pub hdr: MuEntityHeader,
qinsoon's avatar
qinsoon committed
12 13 14 15 16 17
    pub v: MuType_
}

impl MuType {
    pub fn new(id: MuID, v: MuType_) -> MuType {
        MuType {
18
            hdr: MuEntityHeader::unnamed(id),
qinsoon's avatar
qinsoon committed
19 20 21 22
            v: v
        }
    }
}
qinsoon's avatar
qinsoon committed
23

qinsoon's avatar
qinsoon committed
24
#[derive(PartialEq, Debug, RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
25
pub enum MuType_ {
qinsoon's avatar
qinsoon committed
26
    /// int <length>
qinsoon's avatar
qinsoon committed
27
    Int          (usize),
qinsoon's avatar
qinsoon committed
28
    /// float
qinsoon's avatar
qinsoon committed
29
    Float,
qinsoon's avatar
qinsoon committed
30
    /// double
qinsoon's avatar
qinsoon committed
31
    Double,
32

qinsoon's avatar
qinsoon committed
33
    /// ref<T>
qinsoon's avatar
qinsoon committed
34
    Ref          (P<MuType>),    // Box is needed for non-recursive enum
qinsoon's avatar
qinsoon committed
35
    /// iref<T>: internal reference
qinsoon's avatar
qinsoon committed
36
    IRef         (P<MuType>),
qinsoon's avatar
qinsoon committed
37
    /// weakref<T>
qinsoon's avatar
qinsoon committed
38
    WeakRef      (P<MuType>),
39

qinsoon's avatar
qinsoon committed
40
    /// uptr<T>: unsafe pointer
qinsoon's avatar
qinsoon committed
41
    UPtr         (P<MuType>),
42

qinsoon's avatar
qinsoon committed
43
    /// struct<T1 T2 ...>
qinsoon's avatar
qinsoon committed
44
    Struct       (MuName),
45

qinsoon's avatar
qinsoon committed
46
    /// array<T length>
qinsoon's avatar
qinsoon committed
47
    Array        (P<MuType>, usize),
48

qinsoon's avatar
qinsoon committed
49
    /// hybrid<F1 F2 ... V>: a hybrid of fixed length parts and a variable length part
qinsoon's avatar
qinsoon committed
50
    Hybrid       (Vec<P<MuType>>, P<MuType>),
51

qinsoon's avatar
qinsoon committed
52
    /// void
qinsoon's avatar
qinsoon committed
53
    Void,
54

qinsoon's avatar
qinsoon committed
55
    /// threadref
qinsoon's avatar
qinsoon committed
56
    ThreadRef,
qinsoon's avatar
qinsoon committed
57
    /// stackref
qinsoon's avatar
qinsoon committed
58
    StackRef,
59

qinsoon's avatar
qinsoon committed
60
    /// tagref64: hold a double or an int or an ref<void>
qinsoon's avatar
qinsoon committed
61
    Tagref64,
62

qinsoon's avatar
qinsoon committed
63
    /// vector<T length>
qinsoon's avatar
qinsoon committed
64
    Vector       (P<MuType>, usize),
65

qinsoon's avatar
qinsoon committed
66
    /// funcref<@sig>
67
    FuncRef      (P<MuFuncSig>),
68

qinsoon's avatar
qinsoon committed
69
    /// ufuncptr<@sig>
70
    UFuncPtr     (P<MuFuncSig>),
qinsoon's avatar
qinsoon committed
71 72
}

qinsoon's avatar
qinsoon committed
73 74 75 76 77 78
impl fmt::Display for MuType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.v)        
    }
}

79
impl fmt::Display for MuType_ {
80 81
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
qinsoon's avatar
qinsoon committed
82 83 84
            &MuType_::Int(n)                          => write!(f, "int<{}>", n),
            &MuType_::Float                           => write!(f, "float"),
            &MuType_::Double                          => write!(f, "double"),
85 86 87 88 89
            &MuType_::Ref(ref ty)                     => write!(f, "ref<{}>", ty),
            &MuType_::IRef(ref ty)                    => write!(f, "iref<{}>", ty),
            &MuType_::WeakRef(ref ty)                 => write!(f, "weakref<{}>", ty),
            &MuType_::UPtr(ref ty)                    => write!(f, "uptr<{}>", ty),
            &MuType_::Array(ref ty, size)             => write!(f, "array<{} {}>", ty, size),
90
            &MuType_::Hybrid(ref fix_tys, ref var_ty) => write!(f, "hybrid<[{}] {}>", vector_as_str(fix_tys), var_ty),
qinsoon's avatar
qinsoon committed
91 92 93 94
            &MuType_::Void                            => write!(f, "void"),
            &MuType_::ThreadRef                       => write!(f, "threadref"),
            &MuType_::StackRef                        => write!(f, "stackref"),
            &MuType_::Tagref64                        => write!(f, "tagref64"),
95 96 97
            &MuType_::Vector(ref ty, size)            => write!(f, "vector<{} {}>", ty, size),
            &MuType_::FuncRef(ref sig)                => write!(f, "funcref<{}>", sig),
            &MuType_::UFuncPtr(ref sig)               => write!(f, "ufuncref<{}>", sig),
98
            &MuType_::Struct(ref tag)                     => write!(f, "{}(struct)", tag)
99 100 101 102
        }
    }
}

qinsoon's avatar
qinsoon committed
103
lazy_static! {
qinsoon's avatar
qinsoon committed
104 105
    /// storing a map from MuName to StructType_
    pub static ref STRUCT_TAG_MAP : RwLock<HashMap<MuName, StructType_>> = RwLock::new(HashMap::new());
qinsoon's avatar
qinsoon committed
106 107
}

qinsoon's avatar
qinsoon committed
108
#[derive(PartialEq, Debug, RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
109
pub struct StructType_ {
qinsoon's avatar
qinsoon committed
110
    tys: Vec<P<MuType>>
qinsoon's avatar
qinsoon committed
111 112
}

113
impl fmt::Display for StructType_ {
114
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
qinsoon's avatar
qinsoon committed
115 116 117
        write!(f, "struct<").unwrap();
        for i in 0..self.tys.len() {
            let ty = &self.tys[i];
118
            write!(f, "{}", ty).unwrap();
qinsoon's avatar
qinsoon committed
119 120 121
            if i != self.tys.len() - 1 {
                write!(f, " ").unwrap();
            }
122
        }
qinsoon's avatar
qinsoon committed
123
        write!(f, ">")
124
    }
125 126
}

qinsoon's avatar
qinsoon committed
127
impl StructType_ {
qinsoon's avatar
qinsoon committed
128
    pub fn set_tys(&mut self, mut list: Vec<P<MuType>>) {
qinsoon's avatar
qinsoon committed
129
        self.tys.clear();
qinsoon's avatar
qinsoon committed
130
        self.tys.append(&mut list);
qinsoon's avatar
qinsoon committed
131
    }
qinsoon's avatar
qinsoon committed
132
    
qinsoon's avatar
qinsoon committed
133
    pub fn get_tys(&self) -> &Vec<P<MuType>> {
qinsoon's avatar
qinsoon committed
134 135
        &self.tys
    }
qinsoon's avatar
qinsoon committed
136 137
}

qinsoon's avatar
qinsoon committed
138 139 140
impl MuType_ {
    pub fn int(len: usize) -> MuType_ {
        MuType_::Int(len)
qinsoon's avatar
qinsoon committed
141
    }
qinsoon's avatar
qinsoon committed
142 143
    pub fn float() -> MuType_ {
        MuType_::Float
qinsoon's avatar
qinsoon committed
144
    }
qinsoon's avatar
qinsoon committed
145 146
    pub fn double() -> MuType_ {
        MuType_::Double
qinsoon's avatar
qinsoon committed
147
    }
qinsoon's avatar
qinsoon committed
148
    pub fn muref(referent: P<MuType>) -> MuType_ {
qinsoon's avatar
qinsoon committed
149
        MuType_::Ref(referent)
qinsoon's avatar
qinsoon committed
150
    }
qinsoon's avatar
qinsoon committed
151
    pub fn iref(referent: P<MuType>) -> MuType_ {
qinsoon's avatar
qinsoon committed
152
        MuType_::IRef(referent)
qinsoon's avatar
qinsoon committed
153
    }
qinsoon's avatar
qinsoon committed
154
    pub fn weakref(referent: P<MuType>) -> MuType_ {
qinsoon's avatar
qinsoon committed
155
        MuType_::WeakRef(referent)
qinsoon's avatar
qinsoon committed
156
    }
qinsoon's avatar
qinsoon committed
157
    pub fn uptr(referent: P<MuType>) -> MuType_ {
qinsoon's avatar
qinsoon committed
158
        MuType_::UPtr(referent)
qinsoon's avatar
qinsoon committed
159
    }
qinsoon's avatar
qinsoon committed
160
    pub fn mustruct_empty(tag: MuName) -> MuType_ {
qinsoon's avatar
qinsoon committed
161
        let struct_ty_ = StructType_{tys: vec![]};
162
        STRUCT_TAG_MAP.write().unwrap().insert(tag.clone(), struct_ty_);
qinsoon's avatar
qinsoon committed
163

qinsoon's avatar
qinsoon committed
164
        MuType_::Struct(tag)
qinsoon's avatar
qinsoon committed
165
    }
qinsoon's avatar
qinsoon committed
166
    pub fn mustruct(tag: MuName, list: Vec<P<MuType>>) -> MuType_ {
qinsoon's avatar
qinsoon committed
167
        let struct_ty_ = StructType_{tys: list};
qinsoon's avatar
qinsoon committed
168 169 170

        // if there is an attempt to use a same tag for different struct,
        // we panic
171
        match STRUCT_TAG_MAP.read().unwrap().get(&tag) {
qinsoon's avatar
qinsoon committed
172 173 174 175 176 177 178 179 180 181
            Some(old_struct_ty_) => {
                if struct_ty_ != *old_struct_ty_ {
                    panic!(format!(
                            "trying to insert {:?} as {}, while the old struct is defined as {:?}",
                            struct_ty_, tag, old_struct_ty_))
                }
            },
            None => {}
        }
        // otherwise, store the tag
182
        STRUCT_TAG_MAP.write().unwrap().insert(tag.clone(), struct_ty_);
qinsoon's avatar
qinsoon committed
183

qinsoon's avatar
qinsoon committed
184
        MuType_::Struct(tag)
qinsoon's avatar
qinsoon committed
185
    }
qinsoon's avatar
qinsoon committed
186
    pub fn array(ty: P<MuType>, len: usize) -> MuType_ {
qinsoon's avatar
qinsoon committed
187
        MuType_::Array(ty, len)
qinsoon's avatar
qinsoon committed
188
    }
qinsoon's avatar
qinsoon committed
189
    pub fn hybrid(fix_tys: Vec<P<MuType>>, var_ty: P<MuType>) -> MuType_ {
qinsoon's avatar
qinsoon committed
190
        MuType_::Hybrid(fix_tys, var_ty)
qinsoon's avatar
qinsoon committed
191
    }
qinsoon's avatar
qinsoon committed
192 193
    pub fn void() -> MuType_ {
        MuType_::Void
qinsoon's avatar
qinsoon committed
194
    }
qinsoon's avatar
qinsoon committed
195 196
    pub fn threadref() -> MuType_ {
        MuType_::ThreadRef
qinsoon's avatar
qinsoon committed
197
    }
qinsoon's avatar
qinsoon committed
198 199
    pub fn stackref() -> MuType_ {
        MuType_::StackRef
qinsoon's avatar
qinsoon committed
200
    }
qinsoon's avatar
qinsoon committed
201 202
    pub fn tagref64() -> MuType_ {
        MuType_::Tagref64
qinsoon's avatar
qinsoon committed
203
    }
qinsoon's avatar
qinsoon committed
204
    pub fn vector(ty: P<MuType>, len: usize) -> MuType_ {
qinsoon's avatar
qinsoon committed
205
        MuType_::Vector(ty, len)
qinsoon's avatar
qinsoon committed
206
    }
207
    pub fn funcref(sig: P<MuFuncSig>) -> MuType_ {
qinsoon's avatar
qinsoon committed
208
        MuType_::FuncRef(sig)
qinsoon's avatar
qinsoon committed
209
    }
210
    pub fn ufuncptr(sig: P<MuFuncSig>) -> MuType_ {
qinsoon's avatar
qinsoon committed
211
        MuType_::UFuncPtr(sig)
qinsoon's avatar
qinsoon committed
212 213 214 215
    }
}

/// is a type floating-point type?
qinsoon's avatar
qinsoon committed
216
pub fn is_fp(ty: &MuType) -> bool {
qinsoon's avatar
qinsoon committed
217
    match ty.v {
qinsoon's avatar
qinsoon committed
218
        MuType_::Float | MuType_::Double => true,
qinsoon's avatar
qinsoon committed
219 220 221 222 223
        _ => false
    }
}

/// is a type raw pointer?
qinsoon's avatar
qinsoon committed
224
pub fn is_ptr(ty: &MuType) -> bool {
qinsoon's avatar
qinsoon committed
225
    match ty.v {
qinsoon's avatar
qinsoon committed
226
        MuType_::UPtr(_) | MuType_::UFuncPtr(_) => true,
qinsoon's avatar
qinsoon committed
227 228 229 230 231
        _ => false
    }
}

/// is a type scalar type?
qinsoon's avatar
qinsoon committed
232
pub fn is_scalar(ty: &MuType) -> bool {
qinsoon's avatar
qinsoon committed
233
    match ty.v {
qinsoon's avatar
qinsoon committed
234 235 236 237 238 239 240 241 242 243 244 245
        MuType_::Int(_)
        | MuType_::Float
        | MuType_::Double
        | MuType_::Ref(_)
        | MuType_::IRef(_)
        | MuType_::WeakRef(_)
        | MuType_::FuncRef(_)
        | MuType_::UFuncPtr(_)
        | MuType_::ThreadRef
        | MuType_::StackRef
        | MuType_::Tagref64
        | MuType_::UPtr(_) => true,
qinsoon's avatar
qinsoon committed
246 247 248 249 250
        _ => false
    }
}

/// is a type traced by the garbage collector?
251
/// Note: An aggregated type is traced if any of its part is traced.
qinsoon's avatar
qinsoon committed
252
pub fn is_traced(ty: &MuType) -> bool {
qinsoon's avatar
qinsoon committed
253
    match ty.v {
qinsoon's avatar
qinsoon committed
254 255 256 257 258 259 260 261 262
        MuType_::Ref(_) => true,
        MuType_::IRef(_) => true,
        MuType_::WeakRef(_) => true,
        MuType_::Array(ref elem_ty, _)
        | MuType_::Vector(ref elem_ty, _) => is_traced(elem_ty),
        MuType_::ThreadRef
        | MuType_::StackRef
        | MuType_::Tagref64 => true,
        MuType_::Hybrid(ref fix_tys, ref var_ty) => {
qinsoon's avatar
qinsoon committed
263 264
            is_traced(var_ty) ||
            fix_tys.into_iter().map(|ty| is_traced(ty))
265
                .fold(false, |ret, this| ret || this)
qinsoon's avatar
qinsoon committed
266
            },
267
        MuType_::Struct(ref tag) => {
qinsoon's avatar
qinsoon committed
268 269 270
            let map = STRUCT_TAG_MAP.read().unwrap();
            let struct_ty = map.get(tag).unwrap();
            let ref field_tys = struct_ty.tys;
271

qinsoon's avatar
qinsoon committed
272 273 274 275 276 277 278 279 280
            field_tys.into_iter().map(|ty| is_traced(&ty))
                .fold(false, |ret, this| ret || this)
        },
        _ => false
    }
}

/// is a type native safe?
/// Note: An aggregated type is native safe if all of its parts are native safe.
qinsoon's avatar
qinsoon committed
281
pub fn is_native_safe(ty: &MuType) -> bool {
qinsoon's avatar
qinsoon committed
282
    match ty.v {
qinsoon's avatar
qinsoon committed
283 284 285 286 287 288 289 290 291
        MuType_::Int(_) => true,
        MuType_::Float => true,
        MuType_::Double => true,
        MuType_::Void => true,
        MuType_::Array(ref elem_ty, _)
        | MuType_::Vector(ref elem_ty, _) => is_native_safe(elem_ty),
        MuType_::UPtr(_) => true,
        MuType_::UFuncPtr(_) => true,
        MuType_::Hybrid(ref fix_tys, ref var_ty) => {
292
            is_native_safe(var_ty) &&
qinsoon's avatar
qinsoon committed
293 294 295
            fix_tys.into_iter().map(|ty| is_native_safe(&ty))
                .fold(true, |ret, this| ret && this)
        },
296
        MuType_::Struct(ref tag) => {
qinsoon's avatar
qinsoon committed
297 298 299
            let map = STRUCT_TAG_MAP.read().unwrap();
            let struct_ty = map.get(tag).unwrap();
            let ref field_tys = struct_ty.tys;
300

qinsoon's avatar
qinsoon committed
301 302 303 304
            field_tys.into_iter().map(|ty| is_native_safe(&ty))
                .fold(true, |ret, this| ret && this)
        },
        _ => false
qinsoon's avatar
qinsoon committed
305 306 307
    }
}

308
pub fn get_referent_ty(ty: &MuType) -> Option<P<MuType>> {
qinsoon's avatar
qinsoon committed
309 310 311 312
    match ty.v {
        MuType_::Ref(ref referent)
        | MuType_::IRef(ref referent)
        | MuType_::WeakRef(ref referent) => Some(referent.clone()),
313 314 315 316
        _ => None
    }
}

qinsoon's avatar
qinsoon committed
317 318 319 320 321 322 323 324 325
macro_rules! is_type (
    ($e:expr, $p:pat) => (
        match $e {
            $p => true,
            _ => false
        }
    )
);

qinsoon's avatar
qinsoon committed
326
#[derive(PartialEq, Debug, RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
327
pub struct MuFuncSig {
328
    pub hdr: MuEntityHeader,
qinsoon's avatar
qinsoon committed
329 330
    pub ret_tys : Vec<P<MuType>>,
    pub arg_tys: Vec<P<MuType>>
331 332
}

333
impl fmt::Display for MuFuncSig {
334
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
335
        write!(f, "[{}] -> [{}]", vector_as_str(&self.ret_tys), vector_as_str(&self.arg_tys))
336 337
    }
}