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 // Test a sample usage pattern for regions. Makes use of the
13 // following features:
15 // - Multiple lifetime parameters
18 #![feature(rustc_private, libc, collections)]
21 extern crate collections
;
24 use TypeStructure
::{TypeInt, TypeFunction}
;
25 use AstKind
::{ExprInt, ExprVar, ExprLambda}
;
26 use arena
::TypedArena
;
27 use std
::collections
::HashMap
;
30 type Type
<'tcx
> = &'tcx TypeStructure
<'tcx
>;
32 #[derive(Copy, Clone, Debug)]
33 enum TypeStructure
<'tcx
> {
35 TypeFunction(Type
<'tcx
>, Type
<'tcx
>),
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
,
48 impl<'tcx
> Eq
for TypeStructure
<'tcx
> {}
50 type TyArena
<'tcx
> = TypedArena
<TypeStructure
<'tcx
>>;
51 type AstArena
<'ast
> = TypedArena
<AstStructure
<'ast
>>;
53 struct TypeContext
<'tcx
, 'ast
> {
54 ty_arena
: &'tcx TyArena
<'tcx
>,
55 types
: Vec
<Type
<'tcx
>> ,
56 type_table
: HashMap
<NodeId
, Type
<'tcx
>>,
58 ast_arena
: &'ast AstArena
<'ast
>,
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
,
67 type_table
: HashMap
::new(),
73 fn add_type(&mut self, s
: TypeStructure
<'tcx
>) -> Type
<'tcx
> {
74 for &ty
in &self.types
{
80 let ty
= self.ty_arena
.alloc(s
);
85 fn set_type(&mut self, id
: NodeId
, ty
: Type
<'tcx
>) -> Type
<'tcx
> {
86 self.type_table
.insert(id
, ty
);
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
})
97 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
102 type Ast
<'ast
> = &'ast AstStructure
<'ast
>;
104 #[derive(Copy, Clone)]
105 struct AstStructure
<'ast
> {
110 #[derive(Copy, Clone)]
114 ExprLambda(Ast
<'ast
>),
117 fn compute_types
<'tcx
,'ast
>(tcx
: &mut TypeContext
<'tcx
,'ast
>,
118 ast
: Ast
<'ast
>) -> Type
<'tcx
>
121 ExprInt
| ExprVar(_
) => {
122 let ty
= tcx
.add_type(TypeInt
);
123 tcx
.set_type(ast
.id
, ty
)
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
)
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
);