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.
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.
12 #![allow(unused_imports)]
14 // Test a sample usage pattern for regions. Makes use of the
15 // following features:
17 // - Multiple lifetime parameters
20 #![feature(rustc_private, libc)]
25 use TypeStructure::{TypeInt, TypeFunction};
26 use AstKind::{ExprInt, ExprVar, ExprLambda};
27 use arena::TypedArena;
28 use std::collections::HashMap;
31 type Type<'tcx> = &'tcx TypeStructure<'tcx>;
33 #[derive(Copy, Clone, Debug)]
34 enum TypeStructure<'tcx> {
36 TypeFunction(Type<'tcx>, Type<'tcx>),
39 impl<'tcx> PartialEq for TypeStructure<'tcx> {
40 fn eq(&self, other: &TypeStructure<'tcx>) -> bool {
41 match (*self, *other) {
42 (TypeInt, TypeInt) => true,
43 (TypeFunction(s_a, s_b), TypeFunction(o_a, o_b)) => *s_a == *o_a && *s_b == *o_b,
49 impl<'tcx> Eq for TypeStructure<'tcx> {}
51 type TyArena<'tcx> = TypedArena<TypeStructure<'tcx>>;
52 type AstArena<'ast> = TypedArena<AstStructure<'ast>>;
54 struct TypeContext<'tcx, 'ast> {
55 ty_arena: &'tcx TyArena<'tcx>,
56 types: Vec<Type<'tcx>> ,
57 type_table: HashMap<NodeId, Type<'tcx>>,
59 ast_arena: &'ast AstArena<'ast>,
63 impl<'tcx,'ast> TypeContext<'tcx, 'ast> {
64 fn new(ty_arena: &'tcx TyArena<'tcx>, ast_arena: &'ast AstArena<'ast>)
65 -> TypeContext<'tcx, 'ast> {
66 TypeContext { ty_arena: ty_arena,
68 type_table: HashMap::new(),
74 fn add_type(&mut self, s: TypeStructure<'tcx>) -> Type<'tcx> {
75 for &ty in &self.types {
81 let ty = self.ty_arena.alloc(s);
86 fn set_type(&mut self, id: NodeId, ty: Type<'tcx>) -> Type<'tcx> {
87 self.type_table.insert(id, ty);
91 fn ast(&mut self, a: AstKind<'ast>) -> Ast<'ast> {
92 let id = self.ast_counter;
93 self.ast_counter += 1;
94 self.ast_arena.alloc(AstStructure { id: NodeId {id:id}, kind: a })
98 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
103 type Ast<'ast> = &'ast AstStructure<'ast>;
105 #[derive(Copy, Clone)]
106 struct AstStructure<'ast> {
111 #[derive(Copy, Clone)]
115 ExprLambda(Ast<'ast>),
118 fn compute_types<'tcx,'ast>(tcx: &mut TypeContext<'tcx,'ast>,
119 ast: Ast<'ast>) -> Type<'tcx>
122 ExprInt | ExprVar(_) => {
123 let ty = tcx.add_type(TypeInt);
124 tcx.set_type(ast.id, ty)
127 let arg_ty = tcx.add_type(TypeInt);
128 let body_ty = compute_types(tcx, ast);
129 let lambda_ty = tcx.add_type(TypeFunction(arg_ty, body_ty));
130 tcx.set_type(ast.id, lambda_ty)
136 let ty_arena = TypedArena::default();
137 let ast_arena = TypedArena::default();
138 let mut tcx = TypeContext::new(&ty_arena, &ast_arena);
139 let ast = tcx.ast(ExprInt);
140 let ty = compute_types(&mut tcx, ast);
141 assert_eq!(*ty, TypeInt);