]> git.lizzy.rs Git - rust.git/blob - src/librustc_metadata/astencode.rs
Fix invalid associated type rendering in rustdoc
[rust.git] / src / librustc_metadata / astencode.rs
1 // Copyright 2012-2015 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 use rustc::hir::intravisit::{Visitor, NestedVisitorMap};
12
13 use encoder::EncodeContext;
14 use schema::*;
15
16 use rustc::hir;
17 use rustc::ty;
18
19 use rustc_serialize::Encodable;
20
21 #[derive(RustcEncodable, RustcDecodable)]
22 pub struct Ast<'tcx> {
23     pub body: Lazy<hir::Body>,
24     pub tables: Lazy<ty::TypeckTables<'tcx>>,
25     pub nested_bodies: LazySeq<hir::Body>,
26     pub rvalue_promotable_to_static: bool,
27 }
28
29 impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
30     pub fn encode_body(&mut self, body_id: hir::BodyId) -> Lazy<Ast<'tcx>> {
31         let body = self.tcx.hir.body(body_id);
32         let lazy_body = self.lazy(body);
33
34         let tables = self.tcx.body_tables(body_id);
35         let lazy_tables = self.lazy(tables);
36
37         let nested_pos = self.position();
38         let nested_count = {
39             let mut visitor = NestedBodyEncodingVisitor {
40                 ecx: self,
41                 count: 0,
42             };
43             visitor.visit_body(body);
44             visitor.count
45         };
46
47         let rvalue_promotable_to_static =
48             self.tcx.rvalue_promotable_to_static.borrow()[&body.value.id];
49
50         self.lazy(&Ast {
51             body: lazy_body,
52             tables: lazy_tables,
53             nested_bodies: LazySeq::with_position_and_length(nested_pos, nested_count),
54             rvalue_promotable_to_static: rvalue_promotable_to_static
55         })
56     }
57 }
58
59 struct NestedBodyEncodingVisitor<'a, 'b: 'a, 'tcx: 'b> {
60     ecx: &'a mut EncodeContext<'b, 'tcx>,
61     count: usize,
62 }
63
64 impl<'a, 'b, 'tcx> Visitor<'tcx> for NestedBodyEncodingVisitor<'a, 'b, 'tcx> {
65     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
66         NestedVisitorMap::None
67     }
68
69     fn visit_nested_body(&mut self, body: hir::BodyId) {
70         let body = self.ecx.tcx.hir.body(body);
71         body.encode(self.ecx).unwrap();
72         self.count += 1;
73
74         self.visit_body(body);
75     }
76 }