]> git.proxmox.com Git - rustc.git/blame - src/librustc_metadata/astencode.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / librustc_metadata / astencode.rs
CommitLineData
c34b1796 1// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
223e47cc
LB
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
32a655c1 11use rustc::hir::intravisit::{Visitor, NestedVisitorMap};
92a42be0 12
7cac9316 13use isolated_encoder::IsolatedEncoder;
9e0c209e 14use schema::*;
92a42be0 15
32a655c1 16use rustc::hir;
ea8adc8c
XL
17use rustc::ty::{self, TyCtxt};
18
19use rustc::ich::Fingerprint;
20use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5bcae85e 21
9e0c209e
SL
22#[derive(RustcEncodable, RustcDecodable)]
23pub struct Ast<'tcx> {
32a655c1
SL
24 pub body: Lazy<hir::Body>,
25 pub tables: Lazy<ty::TypeckTables<'tcx>>,
26 pub nested_bodies: LazySeq<hir::Body>,
27 pub rvalue_promotable_to_static: bool,
ea8adc8c 28 pub stable_bodies_hash: Fingerprint,
223e47cc
LB
29}
30
cc61c64b
XL
31impl_stable_hash_for!(struct Ast<'tcx> {
32 body,
33 tables,
34 nested_bodies,
ea8adc8c
XL
35 rvalue_promotable_to_static,
36 stable_bodies_hash
cc61c64b
XL
37});
38
7cac9316 39impl<'a, 'b, 'tcx> IsolatedEncoder<'a, 'b, 'tcx> {
32a655c1
SL
40 pub fn encode_body(&mut self, body_id: hir::BodyId) -> Lazy<Ast<'tcx>> {
41 let body = self.tcx.hir.body(body_id);
223e47cc 42
ea8adc8c
XL
43 // In order to avoid having to hash hir::Bodies from extern crates, we
44 // hash them here, during export, and store the hash with metadata.
45 let stable_bodies_hash = {
46 let mut hcx = self.tcx.create_stable_hashing_context();
47 let mut hasher = StableHasher::new();
48
49 hcx.while_hashing_hir_bodies(true, |hcx| {
50 hcx.while_hashing_spans(false, |hcx| {
51 body.hash_stable(hcx, &mut hasher);
52 });
53 });
54
55 hasher.finish()
56 };
57
58 let lazy_body = self.lazy(body);
abe05a73
XL
59 let body_owner_def_id = self.tcx.hir.body_owner_def_id(body_id);
60 let tables = self.tcx.typeck_tables_of(body_owner_def_id);
32a655c1 61 let lazy_tables = self.lazy(tables);
9e0c209e 62
cc61c64b
XL
63 let mut visitor = NestedBodyCollector {
64 tcx: self.tcx,
65 bodies_found: Vec::new(),
9e0c209e 66 };
cc61c64b
XL
67 visitor.visit_body(body);
68 let lazy_nested_bodies = self.lazy_seq_ref_from_slice(&visitor.bodies_found);
1a4d82fc 69
32a655c1 70 let rvalue_promotable_to_static =
abe05a73 71 self.tcx.const_is_rvalue_promotable_to_static(body_owner_def_id);
32a655c1 72
9e0c209e 73 self.lazy(&Ast {
32a655c1
SL
74 body: lazy_body,
75 tables: lazy_tables,
cc61c64b 76 nested_bodies: lazy_nested_bodies,
3b2f2976 77 rvalue_promotable_to_static,
ea8adc8c 78 stable_bodies_hash,
9e0c209e 79 })
1a4d82fc
JJ
80 }
81}
82
cc61c64b 83struct NestedBodyCollector<'a, 'tcx: 'a> {
ea8adc8c 84 tcx: TyCtxt<'a, 'tcx, 'tcx>,
cc61c64b 85 bodies_found: Vec<&'tcx hir::Body>,
1a4d82fc
JJ
86}
87
cc61c64b 88impl<'a, 'tcx: 'a> Visitor<'tcx> for NestedBodyCollector<'a, 'tcx> {
476ff2be 89 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
32a655c1 90 NestedVisitorMap::None
223e47cc 91 }
223e47cc 92
32a655c1 93 fn visit_nested_body(&mut self, body: hir::BodyId) {
cc61c64b
XL
94 let body = self.tcx.hir.body(body);
95 self.bodies_found.push(body);
32a655c1 96 self.visit_body(body);
9e0c209e 97 }
223e47cc 98}