]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/regions-mock-tcx.rs
auto merge of #13049 : alexcrichton/rust/io-fill, r=huonw
[rust.git] / src / test / run-pass / regions-mock-tcx.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // ignore-fast `use` standards don't resolve
12
13 // Test a sample usage pattern for regions. Makes use of the
14 // following features:
15 //
16 // - Multiple lifetime parameters
17 // - Arenas
18
19 extern crate arena;
20 extern crate collections;
21
22 use arena::Arena;
23 use collections::HashMap;
24 use std::cast;
25 use std::libc;
26 use std::mem;
27
28 type Type<'tcx> = &'tcx TypeStructure<'tcx>;
29
30 #[deriving(Show)]
31 enum TypeStructure<'tcx> {
32     TypeInt,
33     TypeFunction(Type<'tcx>, Type<'tcx>),
34 }
35 impl<'tcx> Eq for TypeStructure<'tcx> {
36     fn eq(&self, other: &TypeStructure<'tcx>) -> bool {
37         match (*self, *other) {
38             (TypeInt, TypeInt) => true,
39             (TypeFunction(s_a, s_b), TypeFunction(o_a, o_b)) => *s_a == *o_a && *s_b == *o_b,
40             _ => false
41         }
42     }
43 }
44
45 impl<'tcx> TotalEq for TypeStructure<'tcx> {}
46
47 struct TypeContext<'tcx, 'ast> {
48     ty_arena: &'tcx Arena,
49     types: Vec<Type<'tcx>> ,
50     type_table: HashMap<NodeId, Type<'tcx>>,
51
52     ast_arena: &'ast Arena,
53     ast_counter: uint,
54 }
55
56 impl<'tcx,'ast> TypeContext<'tcx, 'ast> {
57     fn new(ty_arena: &'tcx Arena, ast_arena: &'ast Arena)
58            -> TypeContext<'tcx, 'ast> {
59         TypeContext { ty_arena: ty_arena,
60                       types: Vec::new(),
61                       type_table: HashMap::new(),
62
63                       ast_arena: ast_arena,
64                       ast_counter: 0 }
65     }
66
67     fn add_type(&mut self, s: TypeStructure<'tcx>) -> Type<'tcx> {
68         for &ty in self.types.iter() {
69             if *ty == s {
70                 return ty;
71             }
72         }
73
74         let ty = self.ty_arena.alloc(|| s);
75         self.types.push(ty);
76         ty
77     }
78
79     fn set_type(&mut self, id: NodeId, ty: Type<'tcx>) -> Type<'tcx> {
80         self.type_table.insert(id, ty);
81         ty
82     }
83
84     fn ast(&mut self, a: AstKind<'ast>) -> Ast<'ast> {
85         let id = self.ast_counter;
86         self.ast_counter += 1;
87         self.ast_arena.alloc(|| AstStructure { id: NodeId {id:id}, kind: a })
88     }
89 }
90
91 #[deriving(Eq, TotalEq, Hash)]
92 struct NodeId {
93     id: uint
94 }
95
96 type Ast<'ast> = &'ast AstStructure<'ast>;
97
98 struct AstStructure<'ast> {
99     id: NodeId,
100     kind: AstKind<'ast>
101 }
102
103 enum AstKind<'ast> {
104     ExprInt,
105     ExprVar(uint),
106     ExprLambda(Ast<'ast>),
107 }
108
109 fn compute_types<'tcx,'ast>(tcx: &mut TypeContext<'tcx,'ast>,
110                             ast: Ast<'ast>) -> Type<'tcx>
111 {
112     match ast.kind {
113         ExprInt | ExprVar(_) => {
114             let ty = tcx.add_type(TypeInt);
115             tcx.set_type(ast.id, ty)
116         }
117         ExprLambda(ast) => {
118             let arg_ty = tcx.add_type(TypeInt);
119             let body_ty = compute_types(tcx, ast);
120             let lambda_ty = tcx.add_type(TypeFunction(arg_ty, body_ty));
121             tcx.set_type(ast.id, lambda_ty)
122         }
123     }
124 }
125
126 pub fn main() {
127     let ty_arena = arena::Arena::new();
128     let ast_arena = arena::Arena::new();
129     let mut tcx = TypeContext::new(&ty_arena, &ast_arena);
130     let ast = tcx.ast(ExprInt);
131     let ty = compute_types(&mut tcx, ast);
132     assert_eq!(*ty, TypeInt);
133 }