4 #![allow(unused_imports)]
6 // Test a sample usage pattern for regions. Makes use of the
9 // - Multiple lifetime parameters
12 #![feature(rustc_private, libc)]
14 extern crate rustc_arena;
17 // Necessary to pull in object code as the rest of the rustc crates are shipped only as rmeta
19 #[allow(unused_extern_crates)]
20 extern crate rustc_driver;
22 use TypeStructure::{TypeInt, TypeFunction};
23 use AstKind::{ExprInt, ExprVar, ExprLambda};
24 use rustc_arena::TypedArena;
25 use std::collections::HashMap;
28 type Type<'tcx> = &'tcx TypeStructure<'tcx>;
30 #[derive(Copy, Clone, Debug)]
31 enum TypeStructure<'tcx> {
33 TypeFunction(Type<'tcx>, Type<'tcx>),
36 impl<'tcx> PartialEq for TypeStructure<'tcx> {
37 fn eq(&self, other: &TypeStructure<'tcx>) -> bool {
38 match (*self, *other) {
39 (TypeInt, TypeInt) => true,
40 (TypeFunction(s_a, s_b), TypeFunction(o_a, o_b)) => *s_a == *o_a && *s_b == *o_b,
46 impl<'tcx> Eq for TypeStructure<'tcx> {}
48 type TyArena<'tcx> = TypedArena<TypeStructure<'tcx>>;
49 type AstArena<'ast> = TypedArena<AstStructure<'ast>>;
51 struct TypeContext<'tcx, 'ast> {
52 ty_arena: &'tcx TyArena<'tcx>,
53 types: Vec<Type<'tcx>> ,
54 type_table: HashMap<NodeId, Type<'tcx>>,
56 ast_arena: &'ast AstArena<'ast>,
60 impl<'tcx,'ast> TypeContext<'tcx, 'ast> {
61 fn new(ty_arena: &'tcx TyArena<'tcx>, ast_arena: &'ast AstArena<'ast>)
62 -> TypeContext<'tcx, 'ast> {
63 TypeContext { ty_arena: ty_arena,
65 type_table: HashMap::new(),
71 fn add_type(&mut self, s: TypeStructure<'tcx>) -> Type<'tcx> {
72 for &ty in &self.types {
78 let ty = self.ty_arena.alloc(s);
83 fn set_type(&mut self, id: NodeId, ty: Type<'tcx>) -> Type<'tcx> {
84 self.type_table.insert(id, ty);
88 fn ast(&mut self, a: AstKind<'ast>) -> Ast<'ast> {
89 let id = self.ast_counter;
90 self.ast_counter += 1;
91 self.ast_arena.alloc(AstStructure { id: NodeId {id:id}, kind: a })
95 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
100 type Ast<'ast> = &'ast AstStructure<'ast>;
102 #[derive(Copy, Clone)]
103 struct AstStructure<'ast> {
108 #[derive(Copy, Clone)]
112 ExprLambda(Ast<'ast>),
115 fn compute_types<'tcx,'ast>(tcx: &mut TypeContext<'tcx,'ast>,
116 ast: Ast<'ast>) -> Type<'tcx>
119 ExprInt | ExprVar(_) => {
120 let ty = tcx.add_type(TypeInt);
121 tcx.set_type(ast.id, ty)
124 let arg_ty = tcx.add_type(TypeInt);
125 let body_ty = compute_types(tcx, ast);
126 let lambda_ty = tcx.add_type(TypeFunction(arg_ty, body_ty));
127 tcx.set_type(ast.id, lambda_ty)
133 let ty_arena = TypedArena::default();
134 let ast_arena = TypedArena::default();
135 let mut tcx = TypeContext::new(&ty_arena, &ast_arena);
136 let ast = tcx.ast(ExprInt);
137 let ty = compute_types(&mut tcx, ast);
138 assert_eq!(*ty, TypeInt);