]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
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 | ||
c34b1796 AL |
18 | #![feature(rustc_private, libc, collections)] |
19 | ||
1a4d82fc JJ |
20 | extern crate arena; |
21 | extern crate collections; | |
22 | extern crate libc; | |
23 | ||
24 | use TypeStructure::{TypeInt, TypeFunction}; | |
25 | use AstKind::{ExprInt, ExprVar, ExprLambda}; | |
85aaf69f | 26 | use arena::TypedArena; |
1a4d82fc JJ |
27 | use std::collections::HashMap; |
28 | use std::mem; | |
29 | ||
30 | type Type<'tcx> = &'tcx TypeStructure<'tcx>; | |
31 | ||
c34b1796 | 32 | #[derive(Copy, Clone, Debug)] |
1a4d82fc JJ |
33 | enum TypeStructure<'tcx> { |
34 | TypeInt, | |
35 | TypeFunction(Type<'tcx>, Type<'tcx>), | |
36 | } | |
37 | ||
1a4d82fc JJ |
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 | ||
85aaf69f SL |
50 | type TyArena<'tcx> = TypedArena<TypeStructure<'tcx>>; |
51 | type AstArena<'ast> = TypedArena<AstStructure<'ast>>; | |
52 | ||
1a4d82fc | 53 | struct TypeContext<'tcx, 'ast> { |
85aaf69f | 54 | ty_arena: &'tcx TyArena<'tcx>, |
1a4d82fc JJ |
55 | types: Vec<Type<'tcx>> , |
56 | type_table: HashMap<NodeId, Type<'tcx>>, | |
57 | ||
85aaf69f | 58 | ast_arena: &'ast AstArena<'ast>, |
c34b1796 | 59 | ast_counter: usize, |
1a4d82fc JJ |
60 | } |
61 | ||
62 | impl<'tcx,'ast> TypeContext<'tcx, 'ast> { | |
85aaf69f | 63 | fn new(ty_arena: &'tcx TyArena<'tcx>, ast_arena: &'ast AstArena<'ast>) |
1a4d82fc JJ |
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> { | |
85aaf69f | 74 | for &ty in &self.types { |
1a4d82fc JJ |
75 | if *ty == s { |
76 | return ty; | |
77 | } | |
78 | } | |
79 | ||
85aaf69f | 80 | let ty = self.ty_arena.alloc(s); |
1a4d82fc JJ |
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; | |
85aaf69f | 93 | self.ast_arena.alloc(AstStructure { id: NodeId {id:id}, kind: a }) |
1a4d82fc JJ |
94 | } |
95 | } | |
96 | ||
c34b1796 | 97 | #[derive(Copy, Clone, PartialEq, Eq, Hash)] |
1a4d82fc | 98 | struct NodeId { |
c34b1796 | 99 | id: usize |
1a4d82fc JJ |
100 | } |
101 | ||
1a4d82fc JJ |
102 | type Ast<'ast> = &'ast AstStructure<'ast>; |
103 | ||
c34b1796 | 104 | #[derive(Copy, Clone)] |
1a4d82fc JJ |
105 | struct AstStructure<'ast> { |
106 | id: NodeId, | |
107 | kind: AstKind<'ast> | |
108 | } | |
109 | ||
c34b1796 | 110 | #[derive(Copy, Clone)] |
1a4d82fc JJ |
111 | enum AstKind<'ast> { |
112 | ExprInt, | |
c34b1796 | 113 | ExprVar(usize), |
1a4d82fc JJ |
114 | ExprLambda(Ast<'ast>), |
115 | } | |
116 | ||
1a4d82fc JJ |
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() { | |
85aaf69f SL |
135 | let ty_arena = TypedArena::new(); |
136 | let ast_arena = TypedArena::new(); | |
1a4d82fc JJ |
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 | } |