To protect your data, the CISO officer has suggested users to enable GitLab 2FA as soon as possible.

types.rs 19.2 KB
Newer Older
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Copyright 2017 The Australian National University
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

qinsoon's avatar
qinsoon committed
15
16
17
use ptr::P;
use ir::*;

qinsoon's avatar
qinsoon committed
18
use utils::POINTER_SIZE;
qinsoon's avatar
qinsoon committed
19
use utils::vec_utils;
20
21

use std::fmt;
22
use utils::LinkedHashMap;
qinsoon's avatar
qinsoon committed
23
use std::sync::RwLock;
qinsoon's avatar
qinsoon committed
24

qinsoon's avatar
qinsoon committed
25
26
27
28
lazy_static! {
    pub static ref ADDRESS_TYPE : P<MuType> = P(
        MuType::new(new_internal_id(), MuType_::int(POINTER_SIZE * 8))
    );
qinsoon's avatar
qinsoon committed
29

30
31
32
33
    pub static ref UINT1_TYPE : P<MuType> = P(
        MuType::new(new_internal_id(), MuType_::int(1))
    );

qinsoon's avatar
qinsoon committed
34
35
36
37
38
39
40
    pub static ref UINT8_TYPE : P<MuType> = P(
        MuType::new(new_internal_id(), MuType_::int(8))
    );

    pub static ref UINT16_TYPE : P<MuType> = P(
        MuType::new(new_internal_id(), MuType_::int(16))
    );
qinsoon's avatar
qinsoon committed
41
42
43
44
45
46
47
48
    
    pub static ref UINT32_TYPE : P<MuType> = P(
        MuType::new(new_internal_id(), MuType_::int(32))
    );
    
    pub static ref UINT64_TYPE : P<MuType> = P(
        MuType::new(new_internal_id(), MuType_::int(64))
    );
49
50
51

    pub static ref UINT128_TYPE : P<MuType> = P(
        MuType::new(new_internal_id(), MuType_::int(128))
qinsoon's avatar
qinsoon committed
52
    );
53

54
    pub static ref FLOAT_TYPE : P<MuType> = P(
55
56
57
        MuType::new(new_internal_id(), MuType_::float())
    );

qinsoon's avatar
qinsoon committed
58
59
60
    pub static ref DOUBLE_TYPE : P<MuType> = P(
        MuType::new(new_internal_id(), MuType_::double())
    );
61
62
63
64

    pub static ref VOID_TYPE : P<MuType> = P(
        MuType::new(new_internal_id(), MuType_::void())
    );
qinsoon's avatar
qinsoon committed
65
66
67
    
    pub static ref INTERNAL_TYPES : Vec<P<MuType>> = vec![
        ADDRESS_TYPE.clone(),
68
        UINT1_TYPE.clone(),
qinsoon's avatar
qinsoon committed
69
70
        UINT8_TYPE.clone(),
        UINT16_TYPE.clone(),
qinsoon's avatar
qinsoon committed
71
72
        UINT32_TYPE.clone(),
        UINT64_TYPE.clone(),
73
74
        UINT128_TYPE.clone(),
        FLOAT_TYPE.clone(),
qinsoon's avatar
qinsoon committed
75
        DOUBLE_TYPE.clone(),
76
        FLOAT_TYPE.clone(),
qinsoon's avatar
qinsoon committed
77
        VOID_TYPE.clone()
qinsoon's avatar
qinsoon committed
78
79
80
    ];    
}

81
82
83
84
85
86
87
88
89
90
91
92
pub fn init_types() {
    {
        let mut map_lock = STRUCT_TAG_MAP.write().unwrap();
        map_lock.clear();
    }

    {
        let mut map_lock = HYBRID_TAG_MAP.write().unwrap();
        map_lock.clear();
    }
}

qinsoon's avatar
qinsoon committed
93
#[derive(PartialEq, Debug, RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
94
pub struct MuType {
95
    pub hdr: MuEntityHeader,
qinsoon's avatar
qinsoon committed
96
97
98
99
100
101
    pub v: MuType_
}

impl MuType {
    pub fn new(id: MuID, v: MuType_) -> MuType {
        MuType {
102
            hdr: MuEntityHeader::unnamed(id),
qinsoon's avatar
qinsoon committed
103
104
105
            v: v
        }
    }
qinsoon's avatar
qinsoon committed
106

qinsoon's avatar
qinsoon committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    pub fn is_struct(&self) -> bool {
        match self.v {
            MuType_::Struct(_) => true,
            _ => false
        }
    }

    pub fn is_hybrid(&self) -> bool {
        match self.v {
            MuType_::Hybrid(_) => true,
            _ => false
        }
    }

121
    pub fn is_ref(&self) -> bool {
qinsoon's avatar
qinsoon committed
122
        match self.v {
123
124
            MuType_::Ref(_) => true,
            _ => false
qinsoon's avatar
qinsoon committed
125
126
127
        }
    }

128
    pub fn is_iref(&self) -> bool {
qinsoon's avatar
qinsoon committed
129
        match self.v {
130
            MuType_::IRef(_) => true,
qinsoon's avatar
qinsoon committed
131
132
133
134
            _ => false
        }
    }

135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
    pub fn is_fp(&self) -> bool {
        match self.v {
            MuType_::Float | MuType_::Double => true,
            _ => false
        }
    }

    pub fn is_float(&self) -> bool {
        match self.v {
            MuType_::Float => true,
            _ => false
        }
    }

    pub fn is_double(&self) -> bool {
        match self.v {
            MuType_::Double => true,
            _ => false
        }
    }

    /// is a type raw pointer?
    pub fn is_ptr(&self) -> bool {
qinsoon's avatar
qinsoon committed
158
        match self.v {
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
            MuType_::UPtr(_) | MuType_::UFuncPtr(_) => true,
            _ => false
        }
    }

    /// this a type reference type (located in heap)?
    pub fn is_reference(&self) -> bool {
        match self.v {
            MuType_::Ref(_)
            | MuType_::IRef(_)
            | MuType_::WeakRef(_) => true,
            _ => false
        }
    }

    /// this is a aggregated type (consited of other types)
    pub fn is_aggregate(&self) -> bool {
        match self.v {
            MuType_::Struct(_)
            | MuType_::Hybrid(_)
            | MuType_::Array(_, _) => true,
            _ => false
        }
    }

    /// is a type scalar type?
    pub fn is_scalar(&self) -> bool {
        match self.v {
            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,
            _ => false
        }
    }

    /// is a type traced by the garbage collector?
    /// Note: An aggregated type is traced if any of its part is traced.
    pub fn is_traced(&self) -> bool {
        match self.v {
            MuType_::Ref(_) => true,
qinsoon's avatar
qinsoon committed
208
            MuType_::IRef(_) => true,
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
            MuType_::WeakRef(_) => true,
            MuType_::Array(ref elem_ty, _)
            | MuType_::Vector(ref elem_ty, _) => elem_ty.is_traced(),
            MuType_::ThreadRef
            | MuType_::StackRef
            | MuType_::Tagref64 => true,
            MuType_::Hybrid(ref tag) => {
                let map = HYBRID_TAG_MAP.read().unwrap();
                let hybrid_ty = map.get(tag).unwrap();

                let ref fix_tys = hybrid_ty.fix_tys;
                let ref var_ty  = hybrid_ty.var_ty;

                var_ty.is_traced() ||
                    fix_tys.into_iter().map(|ty| ty.is_traced())
                        .fold(false, |ret, this| ret || this)
            },
            MuType_::Struct(ref tag) => {
                let map = STRUCT_TAG_MAP.read().unwrap();
                let struct_ty = map.get(tag).unwrap();
                let ref field_tys = struct_ty.tys;

                field_tys.into_iter().map(|ty| ty.is_traced())
                    .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.
    pub fn is_native_safe(&self) -> bool {
        match self.v {
            MuType_::Int(_) => true,
            MuType_::Float => true,
            MuType_::Double => true,
            MuType_::Void => true,
            MuType_::Array(ref elem_ty, _)
            | MuType_::Vector(ref elem_ty, _) => elem_ty.is_native_safe(),
            MuType_::UPtr(_) => true,
            MuType_::UFuncPtr(_) => true,
            MuType_::Hybrid(ref tag) => {
                let map = HYBRID_TAG_MAP.read().unwrap();
                let hybrid_ty = map.get(tag).unwrap();

                let ref fix_tys = hybrid_ty.fix_tys;
                let ref var_ty  = hybrid_ty.var_ty;

                var_ty.is_native_safe() &&
                    fix_tys.into_iter().map(|ty| ty.is_native_safe())
                        .fold(true, |ret, this| ret && this)
            },
            MuType_::Struct(ref tag) => {
                let map = STRUCT_TAG_MAP.read().unwrap();
                let struct_ty = map.get(tag).unwrap();
                let ref field_tys = struct_ty.tys;

                field_tys.into_iter().map(|ty| ty.is_native_safe())
                    .fold(true, |ret, this| ret && this)
            },
qinsoon's avatar
qinsoon committed
269
270
271
272
            _ => false
        }
    }

273
274
275
276
277
278
279
280
    pub fn get_struct_hybrid_tag(&self) -> Option<MuName> {
        match self.v {
            MuType_::Hybrid(ref name)
            | MuType_::Struct(ref name) => Some(name.clone()),
            _ => None
        }
    }

qinsoon's avatar
qinsoon committed
281
282
283
284
285
286
287
    pub fn get_elem_ty(&self) -> Option<P<MuType>> {
        match self.v {
            MuType_::Array(ref elem_ty, _) => Some(elem_ty.clone()),
            _ => None
        }
    }

qinsoon's avatar
qinsoon committed
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
    pub fn get_field_ty(&self, index: usize) -> Option<P<MuType>> {
        match self.v {
            MuType_::Struct(ref tag) => {
                let map_lock = STRUCT_TAG_MAP.read().unwrap();
                let struct_inner = map_lock.get(tag).unwrap();

                Some(struct_inner.tys[index].clone())
            },
            MuType_::Hybrid(ref tag) => {
                let map_lock = HYBRID_TAG_MAP.read().unwrap();
                let hybrid_inner = map_lock.get(tag).unwrap();

                Some(hybrid_inner.fix_tys[index].clone())
            },
            _ => None
        }
    }

qinsoon's avatar
qinsoon committed
306
307
308
309
310
311
312
313
314
315
316
317
    pub fn get_hybrid_varpart_ty(&self) -> Option<P<MuType>> {
        match self.v {
            MuType_::Hybrid(ref tag) => {
                let map_lock = HYBRID_TAG_MAP.read().unwrap();
                let hybrid_inner = map_lock.get(tag).unwrap();

                Some(hybrid_inner.var_ty.clone())
            },
            _ => None
        }
    }

qinsoon's avatar
qinsoon committed
318
319
320
321
322
323
324
325
326
327
    pub fn get_referenced_ty(&self) -> Option<P<MuType>> {
        use types::MuType_::*;
        match self.v {
            Ref(ref ty)
            | IRef(ref ty)
            | WeakRef(ref ty)
            | UPtr(ref ty) => Some(ty.clone()),
            _ => None
        }
    }
qinsoon's avatar
qinsoon committed
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344

    pub fn get_int_length(&self) -> Option<usize> {
        use types::MuType_::*;
        match self.v {
            Int(len) => Some(len),
            Ref(_)
            | IRef(_)
            | WeakRef(_)
            | UPtr(_)
            | ThreadRef
            | StackRef
            | Tagref64
            | FuncRef(_)
            | UFuncPtr(_) => Some(64),
            _ => None
        }
    }
qinsoon's avatar
qinsoon committed
345
}
qinsoon's avatar
qinsoon committed
346

347
348
pub type StructTag = MuName;
pub type HybridTag = MuName;
qinsoon's avatar
qinsoon committed
349
#[derive(PartialEq, Debug, RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
350
pub enum MuType_ {
qinsoon's avatar
qinsoon committed
351
    /// int <length>
qinsoon's avatar
qinsoon committed
352
    Int          (usize),
qinsoon's avatar
qinsoon committed
353
    /// float
qinsoon's avatar
qinsoon committed
354
    Float,
qinsoon's avatar
qinsoon committed
355
    /// double
qinsoon's avatar
qinsoon committed
356
    Double,
qinsoon's avatar
qinsoon committed
357

qinsoon's avatar
qinsoon committed
358
    /// ref<T>
qinsoon's avatar
qinsoon committed
359
    Ref          (P<MuType>),    // Box is needed for non-recursive enum
qinsoon's avatar
qinsoon committed
360
    /// iref<T>: internal reference
qinsoon's avatar
qinsoon committed
361
    IRef         (P<MuType>),
qinsoon's avatar
qinsoon committed
362
    /// weakref<T>
qinsoon's avatar
qinsoon committed
363
    WeakRef      (P<MuType>),
qinsoon's avatar
qinsoon committed
364

qinsoon's avatar
qinsoon committed
365
    /// uptr<T>: unsafe pointer
qinsoon's avatar
qinsoon committed
366
    UPtr         (P<MuType>),
qinsoon's avatar
qinsoon committed
367

qinsoon's avatar
qinsoon committed
368
    /// struct<T1 T2 ...>
369
    Struct       (StructTag),
qinsoon's avatar
qinsoon committed
370

qinsoon's avatar
qinsoon committed
371
    /// array<T length>
qinsoon's avatar
qinsoon committed
372
    Array        (P<MuType>, usize),
qinsoon's avatar
qinsoon committed
373

qinsoon's avatar
qinsoon committed
374
    /// hybrid<F1 F2 ... V>: a hybrid of fixed length parts and a variable length part
375
    Hybrid       (HybridTag),
qinsoon's avatar
qinsoon committed
376

qinsoon's avatar
qinsoon committed
377
    /// void
qinsoon's avatar
qinsoon committed
378
    Void,
qinsoon's avatar
qinsoon committed
379

qinsoon's avatar
qinsoon committed
380
    /// threadref
qinsoon's avatar
qinsoon committed
381
    ThreadRef,
qinsoon's avatar
qinsoon committed
382
    /// stackref
qinsoon's avatar
qinsoon committed
383
    StackRef,
qinsoon's avatar
qinsoon committed
384

qinsoon's avatar
qinsoon committed
385
    /// tagref64: hold a double or an int or an ref<void>
qinsoon's avatar
qinsoon committed
386
    Tagref64,
qinsoon's avatar
qinsoon committed
387

qinsoon's avatar
qinsoon committed
388
    /// vector<T length>
qinsoon's avatar
qinsoon committed
389
    Vector       (P<MuType>, usize),
qinsoon's avatar
qinsoon committed
390

qinsoon's avatar
qinsoon committed
391
    /// funcref<@sig>
392
    FuncRef      (P<MuFuncSig>),
qinsoon's avatar
qinsoon committed
393

qinsoon's avatar
qinsoon committed
394
    /// ufuncptr<@sig>
395
    UFuncPtr     (P<MuFuncSig>),
qinsoon's avatar
qinsoon committed
396
397
}

qinsoon's avatar
qinsoon committed
398
399
400
401
402
403
impl fmt::Display for MuType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.v)        
    }
}

404
impl fmt::Display for MuType_ {
405
406
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
qinsoon's avatar
qinsoon committed
407
408
409
            &MuType_::Int(n)                          => write!(f, "int<{}>", n),
            &MuType_::Float                           => write!(f, "float"),
            &MuType_::Double                          => write!(f, "double"),
410
411
412
413
414
            &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),
qinsoon's avatar
qinsoon committed
415
416
417
418
            &MuType_::Void                            => write!(f, "void"),
            &MuType_::ThreadRef                       => write!(f, "threadref"),
            &MuType_::StackRef                        => write!(f, "stackref"),
            &MuType_::Tagref64                        => write!(f, "tagref64"),
419
420
421
            &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),
422
423
            &MuType_::Struct(ref tag)                 => write!(f, "{}(struct)", tag),
            &MuType_::Hybrid(ref tag)                 => write!(f, "{}(hybrid)", tag)
424
425
426
427
        }
    }
}

qinsoon's avatar
qinsoon committed
428
lazy_static! {
qinsoon's avatar
qinsoon committed
429
    /// storing a map from MuName to StructType_
430
    pub static ref STRUCT_TAG_MAP : RwLock<LinkedHashMap<StructTag, StructType_>> = RwLock::new(LinkedHashMap::new());
431
    /// storing a map from MuName to HybridType_
432
    pub static ref HYBRID_TAG_MAP : RwLock<LinkedHashMap<HybridTag, HybridType_>> = RwLock::new(LinkedHashMap::new());
qinsoon's avatar
qinsoon committed
433
434
}

qinsoon's avatar
qinsoon committed
435
#[derive(PartialEq, Debug, RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
436
pub struct StructType_ {
qinsoon's avatar
qinsoon committed
437
    tys: Vec<P<MuType>>
qinsoon's avatar
qinsoon committed
438
439
}

440
impl fmt::Display for StructType_ {
441
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
qinsoon's avatar
qinsoon committed
442
443
444
        write!(f, "struct<").unwrap();
        for i in 0..self.tys.len() {
            let ty = &self.tys[i];
445
            write!(f, "{}", ty).unwrap();
qinsoon's avatar
qinsoon committed
446
447
448
            if i != self.tys.len() - 1 {
                write!(f, " ").unwrap();
            }
449
        }
qinsoon's avatar
qinsoon committed
450
        write!(f, ">")
qinsoon's avatar
qinsoon committed
451
    }
452
453
}

qinsoon's avatar
qinsoon committed
454
impl StructType_ {
455
456
457
458
    // The IR builder needs to create StructType objects, too.
    pub fn new(tys: Vec<P<MuType>>) -> StructType_ {
        StructType_ { tys: tys }
    }
qinsoon's avatar
qinsoon committed
459
    pub fn set_tys(&mut self, mut list: Vec<P<MuType>>) {
qinsoon's avatar
qinsoon committed
460
        self.tys.clear();
qinsoon's avatar
qinsoon committed
461
        self.tys.append(&mut list);
qinsoon's avatar
qinsoon committed
462
    }
qinsoon's avatar
qinsoon committed
463
    
qinsoon's avatar
qinsoon committed
464
    pub fn get_tys(&self) -> &Vec<P<MuType>> {
qinsoon's avatar
qinsoon committed
465
466
        &self.tys
    }
qinsoon's avatar
qinsoon committed
467
468
}

469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#[derive(PartialEq, Debug, RustcEncodable, RustcDecodable)]
pub struct HybridType_ {
    fix_tys: Vec<P<MuType>>,
    var_ty : P<MuType>
}

impl HybridType_ {
    pub fn new(fix_tys: Vec<P<MuType>>, var_ty: P<MuType>) -> HybridType_ {
        HybridType_ {fix_tys: fix_tys, var_ty: var_ty}
    }

    pub fn set_tys(&mut self, mut fix_tys: Vec<P<MuType>>, var_ty: P<MuType>) {
        self.fix_tys.clear();
        self.fix_tys.append(&mut fix_tys);

        self.var_ty = var_ty;
    }

    pub fn get_fix_tys(&self) -> &Vec<P<MuType>> {
        &self.fix_tys
    }

    pub fn get_var_ty(&self) -> &P<MuType> {
        &self.var_ty
    }
}

impl fmt::Display for HybridType_ {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "hybrid<").unwrap();
        for i in 0..self.fix_tys.len() {
            let ty = &self.fix_tys[i];
            write!(f, "{}", ty).unwrap();
            if i != self.fix_tys.len() - 1 {
                write!(f, " ").unwrap();
            }
        }
        write!(f, "|{}>", self.var_ty)
    }
}

qinsoon's avatar
qinsoon committed
510
511
512
impl MuType_ {
    pub fn int(len: usize) -> MuType_ {
        MuType_::Int(len)
qinsoon's avatar
qinsoon committed
513
    }
qinsoon's avatar
qinsoon committed
514
515
    pub fn float() -> MuType_ {
        MuType_::Float
qinsoon's avatar
qinsoon committed
516
    }
qinsoon's avatar
qinsoon committed
517
518
    pub fn double() -> MuType_ {
        MuType_::Double
qinsoon's avatar
qinsoon committed
519
    }
qinsoon's avatar
qinsoon committed
520
    pub fn muref(referent: P<MuType>) -> MuType_ {
qinsoon's avatar
qinsoon committed
521
        MuType_::Ref(referent)
qinsoon's avatar
qinsoon committed
522
    }
qinsoon's avatar
qinsoon committed
523
    pub fn iref(referent: P<MuType>) -> MuType_ {
qinsoon's avatar
qinsoon committed
524
        MuType_::IRef(referent)
qinsoon's avatar
qinsoon committed
525
    }
qinsoon's avatar
qinsoon committed
526
    pub fn weakref(referent: P<MuType>) -> MuType_ {
qinsoon's avatar
qinsoon committed
527
        MuType_::WeakRef(referent)
qinsoon's avatar
qinsoon committed
528
    }
qinsoon's avatar
qinsoon committed
529
    pub fn uptr(referent: P<MuType>) -> MuType_ {
qinsoon's avatar
qinsoon committed
530
        MuType_::UPtr(referent)
qinsoon's avatar
qinsoon committed
531
    }
qinsoon's avatar
qinsoon committed
532
    pub fn mustruct_empty(tag: MuName) -> MuType_ {
qinsoon's avatar
qinsoon committed
533
        let struct_ty_ = StructType_{tys: vec![]};
534
        STRUCT_TAG_MAP.write().unwrap().insert(tag.clone(), struct_ty_);
qinsoon's avatar
qinsoon committed
535

qinsoon's avatar
qinsoon committed
536
        MuType_::Struct(tag)
qinsoon's avatar
qinsoon committed
537
    }
538
    pub fn mustruct_put(tag: &MuName, mut list: Vec<P<MuType>>) {
539
540
        let mut map_guard = STRUCT_TAG_MAP.write().unwrap();

541
        match map_guard.get_mut(tag) {
542
543
544
545
546
547
548
549
            Some(struct_ty_) => {
                struct_ty_.tys.clear();
                struct_ty_.tys.append(&mut list);
            },
            None => panic!("call mustruct_empty() to create an empty struct before mustruct_put()")
        }
    }
    pub fn mustruct(tag: StructTag, list: Vec<P<MuType>>) -> MuType_ {
qinsoon's avatar
qinsoon committed
550
        let struct_ty_ = StructType_{tys: list};
qinsoon's avatar
qinsoon committed
551
552
553

        // if there is an attempt to use a same tag for different struct,
        // we panic
554
        match STRUCT_TAG_MAP.read().unwrap().get(&tag) {
qinsoon's avatar
qinsoon committed
555
556
            Some(old_struct_ty_) => {
                if struct_ty_ != *old_struct_ty_ {
557
558
                    panic!("trying to insert {} as {}, while the old struct is defined as {}",
                            struct_ty_, tag, old_struct_ty_)
qinsoon's avatar
qinsoon committed
559
560
561
562
563
                }
            },
            None => {}
        }
        // otherwise, store the tag
564
        STRUCT_TAG_MAP.write().unwrap().insert(tag.clone(), struct_ty_);
qinsoon's avatar
qinsoon committed
565

qinsoon's avatar
qinsoon committed
566
        MuType_::Struct(tag)
qinsoon's avatar
qinsoon committed
567
    }
qinsoon's avatar
qinsoon committed
568
    pub fn array(ty: P<MuType>, len: usize) -> MuType_ {
qinsoon's avatar
qinsoon committed
569
        MuType_::Array(ty, len)
qinsoon's avatar
qinsoon committed
570
    }
571
572
573
574
575
576
    pub fn hybrid_empty(tag: HybridTag) -> MuType_ {
        let hybrid_ty_ = HybridType_{fix_tys: vec![], var_ty: VOID_TYPE.clone()};
        HYBRID_TAG_MAP.write().unwrap().insert(tag.clone(), hybrid_ty_);

        MuType_::Hybrid(tag)
    }
577
    pub fn hybrid_put(tag: &HybridTag, mut fix_tys: Vec<P<MuType>>, var_ty: P<MuType>) {
qinsoon's avatar
qinsoon committed
578
579
        let mut map_guard = HYBRID_TAG_MAP.write().unwrap();

580
        match map_guard.get_mut(tag) {
qinsoon's avatar
qinsoon committed
581
582
583
584
585
586
587
588
589
            Some(hybrid_ty_) => {
                hybrid_ty_.fix_tys.clear();
                hybrid_ty_.fix_tys.append(&mut fix_tys);

                hybrid_ty_.var_ty = var_ty;
            },
            None => panic!("call hybrid_empty() to create an empty struct before hybrid_put()")
        }
    }
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
    pub fn hybrid(tag: HybridTag, fix_tys: Vec<P<MuType>>, var_ty: P<MuType>) -> MuType_ {
        let hybrid_ty_ = HybridType_{fix_tys: fix_tys, var_ty: var_ty};


        match HYBRID_TAG_MAP.read().unwrap().get(&tag) {
            Some(old_hybrid_ty_) => {
                if hybrid_ty_ != *old_hybrid_ty_ {
                    panic!("trying to insert {} as {}, while the old hybrid is defined as {}",
                           hybrid_ty_, tag, old_hybrid_ty_);
                }
            },
            None => {}
        }

        HYBRID_TAG_MAP.write().unwrap().insert(tag.clone(), hybrid_ty_);

        MuType_::Hybrid(tag)
qinsoon's avatar
qinsoon committed
607
    }
qinsoon's avatar
qinsoon committed
608
609
    pub fn void() -> MuType_ {
        MuType_::Void
qinsoon's avatar
qinsoon committed
610
    }
qinsoon's avatar
qinsoon committed
611
612
    pub fn threadref() -> MuType_ {
        MuType_::ThreadRef
qinsoon's avatar
qinsoon committed
613
    }
qinsoon's avatar
qinsoon committed
614
615
    pub fn stackref() -> MuType_ {
        MuType_::StackRef
qinsoon's avatar
qinsoon committed
616
    }
qinsoon's avatar
qinsoon committed
617
618
    pub fn tagref64() -> MuType_ {
        MuType_::Tagref64
qinsoon's avatar
qinsoon committed
619
    }
qinsoon's avatar
qinsoon committed
620
    pub fn vector(ty: P<MuType>, len: usize) -> MuType_ {
qinsoon's avatar
qinsoon committed
621
        MuType_::Vector(ty, len)
qinsoon's avatar
qinsoon committed
622
    }
623
    pub fn funcref(sig: P<MuFuncSig>) -> MuType_ {
qinsoon's avatar
qinsoon committed
624
        MuType_::FuncRef(sig)
qinsoon's avatar
qinsoon committed
625
    }
626
    pub fn ufuncptr(sig: P<MuFuncSig>) -> MuType_ {
qinsoon's avatar
qinsoon committed
627
        MuType_::UFuncPtr(sig)
qinsoon's avatar
qinsoon committed
628
629
630
    }
}

631
pub fn get_referent_ty(ty: &MuType) -> Option<P<MuType>> {
qinsoon's avatar
qinsoon committed
632
633
634
    match ty.v {
        MuType_::Ref(ref referent)
        | MuType_::IRef(ref referent)
qinsoon's avatar
qinsoon committed
635
636
        | MuType_::WeakRef(ref referent)
        | MuType_::UPtr(ref referent) => Some(referent.clone()),
637
638
639
640
        _ => None
    }
}

qinsoon's avatar
qinsoon committed
641
642
643
644
645
646
647
648
649
macro_rules! is_type (
    ($e:expr, $p:pat) => (
        match $e {
            $p => true,
            _ => false
        }
    )
);

650
651
pub type CFuncSig = MuFuncSig;

qinsoon's avatar
qinsoon committed
652
#[derive(PartialEq, Debug, RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
653
pub struct MuFuncSig {
654
    pub hdr: MuEntityHeader,
qinsoon's avatar
qinsoon committed
655
656
    pub ret_tys : Vec<P<MuType>>,
    pub arg_tys: Vec<P<MuType>>
657
658
}

659
impl fmt::Display for MuFuncSig {
660
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
qinsoon's avatar
qinsoon committed
661
        write!(f, "[{}] -> [{}]", vec_utils::as_str(&self.arg_tys), vec_utils::as_str(&self.ret_tys))
qinsoon's avatar
qinsoon committed
662
663
    }
}