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.
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.
11 use rustc::hir::intravisit::{Visitor, NestedVisitorMap};
13 use encoder::EncodeContext;
19 use rustc_serialize::Encodable;
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,
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);
34 let tables = self.tcx.body_tables(body_id);
35 let lazy_tables = self.lazy(tables);
37 let nested_pos = self.position();
39 let mut visitor = NestedBodyEncodingVisitor {
43 visitor.visit_body(body);
47 let rvalue_promotable_to_static =
48 self.tcx.rvalue_promotable_to_static.borrow()[&body.value.id];
53 nested_bodies: LazySeq::with_position_and_length(nested_pos, nested_count),
54 rvalue_promotable_to_static: rvalue_promotable_to_static
59 struct NestedBodyEncodingVisitor<'a, 'b: 'a, 'tcx: 'b> {
60 ecx: &'a mut EncodeContext<'b, 'tcx>,
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
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();
74 self.visit_body(body);