]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/regions-mock-tcx.rs
rand: Use fill() instead of read()
[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 struct TypeContext<'tcx, 'ast> {
46     ty_arena: &'tcx Arena,
47     types: Vec<Type<'tcx>> ,
48     type_table: HashMap<NodeId, Type<'tcx>>,
49
50     ast_arena: &'ast Arena,
51     ast_counter: uint,
52 }
53
54 impl<'tcx,'ast> TypeContext<'tcx, 'ast> {
55     fn new(ty_arena: &'tcx Arena, ast_arena: &'ast Arena)
56            -> TypeContext<'tcx, 'ast> {
57         TypeContext { ty_arena: ty_arena,
58                       types: Vec::new(),
59                       type_table: HashMap::new(),
60
61                       ast_arena: ast_arena,
62                       ast_counter: 0 }
63     }
64
65     fn add_type(&mut self, s: TypeStructure<'tcx>) -> Type<'tcx> {
66         for &ty in self.types.iter() {
67             if *ty == s {
68                 return ty;
69             }
70         }
71
72         let ty = self.ty_arena.alloc(|| s);
73         self.types.push(ty);
74         ty
75     }
76
77     fn set_type(&mut self, id: NodeId, ty: Type<'tcx>) -> Type<'tcx> {
78         self.type_table.insert(id, ty);
79         ty
80     }
81
82     fn ast(&mut self, a: AstKind<'ast>) -> Ast<'ast> {
83         let id = self.ast_counter;
84         self.ast_counter += 1;
85         self.ast_arena.alloc(|| AstStructure { id: NodeId {id:id}, kind: a })
86     }
87 }
88
89 #[deriving(Eq, Hash)]
90 struct NodeId {
91     id: uint
92 }
93
94 type Ast<'ast> = &'ast AstStructure<'ast>;
95
96 struct AstStructure<'ast> {
97     id: NodeId,
98     kind: AstKind<'ast>
99 }
100
101 enum AstKind<'ast> {
102     ExprInt,
103     ExprVar(uint),
104     ExprLambda(Ast<'ast>),
105 }
106
107 fn compute_types<'tcx,'ast>(tcx: &mut TypeContext<'tcx,'ast>,
108                             ast: Ast<'ast>) -> Type<'tcx>
109 {
110     match ast.kind {
111         ExprInt | ExprVar(_) => {
112             let ty = tcx.add_type(TypeInt);
113             tcx.set_type(ast.id, ty)
114         }
115         ExprLambda(ast) => {
116             let arg_ty = tcx.add_type(TypeInt);
117             let body_ty = compute_types(tcx, ast);
118             let lambda_ty = tcx.add_type(TypeFunction(arg_ty, body_ty));
119             tcx.set_type(ast.id, lambda_ty)
120         }
121     }
122 }
123
124 pub fn main() {
125     let ty_arena = arena::Arena::new();
126     let ast_arena = arena::Arena::new();
127     let mut tcx = TypeContext::new(&ty_arena, &ast_arena);
128     let ast = tcx.ast(ExprInt);
129     let ty = compute_types(&mut tcx, ast);
130     assert_eq!(*ty, TypeInt);
131 }