]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/regions-mock-tcx.rs
Imported Upstream version 1.8.0+dfsg1
[rustc.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
12 // Test a sample usage pattern for regions. Makes use of the
13 // following features:
14 //
15 // - Multiple lifetime parameters
16 // - Arenas
17
18 #![feature(rustc_private, libc, collections)]
19
20 extern crate arena;
21 extern crate collections;
22 extern crate libc;
23
24 use TypeStructure::{TypeInt, TypeFunction};
25 use AstKind::{ExprInt, ExprVar, ExprLambda};
26 use arena::TypedArena;
27 use std::collections::HashMap;
28 use std::mem;
29
30 type Type<'tcx> = &'tcx TypeStructure<'tcx>;
31
32 #[derive(Copy, Clone, Debug)]
33 enum TypeStructure<'tcx> {
34 TypeInt,
35 TypeFunction(Type<'tcx>, Type<'tcx>),
36 }
37
38 impl<'tcx> PartialEq for TypeStructure<'tcx> {
39 fn eq(&self, other: &TypeStructure<'tcx>) -> bool {
40 match (*self, *other) {
41 (TypeInt, TypeInt) => true,
42 (TypeFunction(s_a, s_b), TypeFunction(o_a, o_b)) => *s_a == *o_a && *s_b == *o_b,
43 _ => false
44 }
45 }
46 }
47
48 impl<'tcx> Eq for TypeStructure<'tcx> {}
49
50 type TyArena<'tcx> = TypedArena<TypeStructure<'tcx>>;
51 type AstArena<'ast> = TypedArena<AstStructure<'ast>>;
52
53 struct TypeContext<'tcx, 'ast> {
54 ty_arena: &'tcx TyArena<'tcx>,
55 types: Vec<Type<'tcx>> ,
56 type_table: HashMap<NodeId, Type<'tcx>>,
57
58 ast_arena: &'ast AstArena<'ast>,
59 ast_counter: usize,
60 }
61
62 impl<'tcx,'ast> TypeContext<'tcx, 'ast> {
63 fn new(ty_arena: &'tcx TyArena<'tcx>, ast_arena: &'ast AstArena<'ast>)
64 -> TypeContext<'tcx, 'ast> {
65 TypeContext { ty_arena: ty_arena,
66 types: Vec::new(),
67 type_table: HashMap::new(),
68
69 ast_arena: ast_arena,
70 ast_counter: 0 }
71 }
72
73 fn add_type(&mut self, s: TypeStructure<'tcx>) -> Type<'tcx> {
74 for &ty in &self.types {
75 if *ty == s {
76 return ty;
77 }
78 }
79
80 let ty = self.ty_arena.alloc(s);
81 self.types.push(ty);
82 ty
83 }
84
85 fn set_type(&mut self, id: NodeId, ty: Type<'tcx>) -> Type<'tcx> {
86 self.type_table.insert(id, ty);
87 ty
88 }
89
90 fn ast(&mut self, a: AstKind<'ast>) -> Ast<'ast> {
91 let id = self.ast_counter;
92 self.ast_counter += 1;
93 self.ast_arena.alloc(AstStructure { id: NodeId {id:id}, kind: a })
94 }
95 }
96
97 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
98 struct NodeId {
99 id: usize
100 }
101
102 type Ast<'ast> = &'ast AstStructure<'ast>;
103
104 #[derive(Copy, Clone)]
105 struct AstStructure<'ast> {
106 id: NodeId,
107 kind: AstKind<'ast>
108 }
109
110 #[derive(Copy, Clone)]
111 enum AstKind<'ast> {
112 ExprInt,
113 ExprVar(usize),
114 ExprLambda(Ast<'ast>),
115 }
116
117 fn compute_types<'tcx,'ast>(tcx: &mut TypeContext<'tcx,'ast>,
118 ast: Ast<'ast>) -> Type<'tcx>
119 {
120 match ast.kind {
121 ExprInt | ExprVar(_) => {
122 let ty = tcx.add_type(TypeInt);
123 tcx.set_type(ast.id, ty)
124 }
125 ExprLambda(ast) => {
126 let arg_ty = tcx.add_type(TypeInt);
127 let body_ty = compute_types(tcx, ast);
128 let lambda_ty = tcx.add_type(TypeFunction(arg_ty, body_ty));
129 tcx.set_type(ast.id, lambda_ty)
130 }
131 }
132 }
133
134 pub fn main() {
135 let ty_arena = TypedArena::new();
136 let ast_arena = TypedArena::new();
137 let mut tcx = TypeContext::new(&ty_arena, &ast_arena);
138 let ast = tcx.ast(ExprInt);
139 let ty = compute_types(&mut tcx, ast);
140 assert_eq!(*ty, TypeInt);
141 }