Skip to content

Commit

Permalink
Do not encode name when encoding DefKey
Browse files Browse the repository at this point in the history
  • Loading branch information
sanxiyn committed Apr 11, 2016
1 parent 23a7d30 commit 2fd1f3e
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 22 deletions.
16 changes: 8 additions & 8 deletions src/librustc_metadata/astencode.rs
Expand Up @@ -49,9 +49,9 @@ use std::fmt::Debug;
use rbml::reader;
use rbml::writer::Encoder;
use rbml;
use serialize;
use serialize::{Decodable, Decoder, DecoderHelpers, Encodable};
use serialize::EncoderHelpers;
use rustc_serialize as serialize;
use rustc_serialize::{Decodable, Decoder, DecoderHelpers};
use rustc_serialize::{Encodable, EncoderHelpers};

#[cfg(test)] use std::io::Cursor;
#[cfg(test)] use syntax::parse;
Expand Down Expand Up @@ -445,7 +445,7 @@ fn encode_method_callee<'a, 'tcx>(ecx: &e::EncodeContext<'a, 'tcx>,
rbml_w: &mut Encoder,
autoderef: u32,
method: &ty::MethodCallee<'tcx>) {
use serialize::Encoder;
use rustc_serialize::Encoder;

rbml_w.emit_struct("MethodCallee", 4, |rbml_w| {
rbml_w.emit_struct_field("autoderef", 0, |rbml_w| {
Expand Down Expand Up @@ -561,7 +561,7 @@ impl<'a, 'tcx> rbml_writer_helpers<'tcx> for Encoder<'a> {
}

fn emit_upvar_capture(&mut self, ecx: &e::EncodeContext, capture: &ty::UpvarCapture) {
use serialize::Encoder;
use rustc_serialize::Encoder;

self.emit_enum("UpvarCapture", |this| {
match *capture {
Expand Down Expand Up @@ -589,7 +589,7 @@ impl<'a, 'tcx> rbml_writer_helpers<'tcx> for Encoder<'a> {

fn emit_auto_adjustment<'b>(&mut self, ecx: &e::EncodeContext<'b, 'tcx>,
adj: &adjustment::AutoAdjustment<'tcx>) {
use serialize::Encoder;
use rustc_serialize::Encoder;

self.emit_enum("AutoAdjustment", |this| {
match *adj {
Expand Down Expand Up @@ -621,7 +621,7 @@ impl<'a, 'tcx> rbml_writer_helpers<'tcx> for Encoder<'a> {

fn emit_autoref<'b>(&mut self, ecx: &e::EncodeContext<'b, 'tcx>,
autoref: &adjustment::AutoRef<'tcx>) {
use serialize::Encoder;
use rustc_serialize::Encoder;

self.emit_enum("AutoRef", |this| {
match autoref {
Expand All @@ -643,7 +643,7 @@ impl<'a, 'tcx> rbml_writer_helpers<'tcx> for Encoder<'a> {

fn emit_auto_deref_ref<'b>(&mut self, ecx: &e::EncodeContext<'b, 'tcx>,
auto_deref_ref: &adjustment::AutoDerefRef<'tcx>) {
use serialize::Encoder;
use rustc_serialize::Encoder;

self.emit_struct("AutoDerefRef", 2, |this| {
this.emit_struct_field("autoderefs", 0, |this| auto_deref_ref.autoderefs.encode(this));
Expand Down
9 changes: 7 additions & 2 deletions src/librustc_metadata/decoder.rs
Expand Up @@ -17,6 +17,7 @@ use self::Family::*;
use astencode::decode_inlined_item;
use cstore::{self, crate_metadata};
use common::*;
use def_key;
use encoder::def_to_u64;
use index;
use tls_context;
Expand Down Expand Up @@ -49,7 +50,7 @@ use std::str;

use rbml::reader;
use rbml;
use serialize::Decodable;
use rustc_serialize::Decodable;
use syntax::attr;
use syntax::parse::token::{self, IdentInterner};
use syntax::ast;
Expand Down Expand Up @@ -1739,7 +1740,11 @@ pub fn def_key(cdata: Cmd, id: DefIndex) -> hir_map::DefKey {
match reader::maybe_get_doc(item_doc, tag_def_key) {
Some(def_key_doc) => {
let mut decoder = reader::Decoder::new(def_key_doc);
hir_map::DefKey::decode(&mut decoder).unwrap()
let simple_key = def_key::DefKey::decode(&mut decoder).unwrap();
let name = reader::maybe_get_doc(item_doc, tag_paths_data_name).map(|name| {
token::intern(name.as_str_slice())
});
def_key::recover_def_key(simple_key, name)
}
None => {
bug!("failed to find block with tag {:?} for item with family {:?}",
Expand Down
104 changes: 104 additions & 0 deletions src/librustc_metadata/def_key.rs
@@ -0,0 +1,104 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use rustc::hir::def_id::DefIndex;
use rustc::hir::map as hir_map;
use syntax::ast::Name;

#[derive(RustcEncodable, RustcDecodable)]
pub struct DefKey {
pub parent: Option<DefIndex>,
pub disambiguated_data: DisambiguatedDefPathData,
}

#[derive(RustcEncodable, RustcDecodable)]
pub struct DisambiguatedDefPathData {
pub data: DefPathData,
pub disambiguator: u32,
}

#[derive(RustcEncodable, RustcDecodable)]
pub enum DefPathData {
CrateRoot,
Misc,
Impl,
TypeNs,
ValueNs,
MacroDef,
ClosureExpr,
TypeParam,
LifetimeDef,
EnumVariant,
Field,
StructCtor,
Initializer,
Binding,
}

pub fn simplify_def_key(key: hir_map::DefKey) -> DefKey {
let data = DisambiguatedDefPathData {
data: simplify_def_path_data(key.disambiguated_data.data),
disambiguator: key.disambiguated_data.disambiguator,
};
DefKey {
parent: key.parent,
disambiguated_data: data,
}
}

fn simplify_def_path_data(data: hir_map::DefPathData) -> DefPathData {
match data {
hir_map::DefPathData::CrateRoot => DefPathData::CrateRoot,
hir_map::DefPathData::InlinedRoot(_) => bug!("unexpected DefPathData"),
hir_map::DefPathData::Misc => DefPathData::Misc,
hir_map::DefPathData::Impl => DefPathData::Impl,
hir_map::DefPathData::TypeNs(_) => DefPathData::TypeNs,
hir_map::DefPathData::ValueNs(_) => DefPathData::ValueNs,
hir_map::DefPathData::MacroDef(_) => DefPathData::MacroDef,
hir_map::DefPathData::ClosureExpr => DefPathData::ClosureExpr,
hir_map::DefPathData::TypeParam(_) => DefPathData::TypeParam,
hir_map::DefPathData::LifetimeDef(_) => DefPathData::LifetimeDef,
hir_map::DefPathData::EnumVariant(_) => DefPathData::EnumVariant,
hir_map::DefPathData::Field(_) => DefPathData::Field,
hir_map::DefPathData::StructCtor => DefPathData::StructCtor,
hir_map::DefPathData::Initializer => DefPathData::Initializer,
hir_map::DefPathData::Binding(_) => DefPathData::Binding,
}
}

pub fn recover_def_key(key: DefKey, name: Option<Name>) -> hir_map::DefKey {
let data = hir_map::DisambiguatedDefPathData {
data: recover_def_path_data(key.disambiguated_data.data, name),
disambiguator: key.disambiguated_data.disambiguator,
};
hir_map::DefKey {
parent: key.parent,
disambiguated_data: data,
}
}

fn recover_def_path_data(data: DefPathData, name: Option<Name>) -> hir_map::DefPathData {
match data {
DefPathData::CrateRoot => hir_map::DefPathData::CrateRoot,
DefPathData::Misc => hir_map::DefPathData::Misc,
DefPathData::Impl => hir_map::DefPathData::Impl,
DefPathData::TypeNs => hir_map::DefPathData::TypeNs(name.unwrap()),
DefPathData::ValueNs => hir_map::DefPathData::ValueNs(name.unwrap()),
DefPathData::MacroDef => hir_map::DefPathData::MacroDef(name.unwrap()),
DefPathData::ClosureExpr => hir_map::DefPathData::ClosureExpr,
DefPathData::TypeParam => hir_map::DefPathData::TypeParam(name.unwrap()),
DefPathData::LifetimeDef => hir_map::DefPathData::LifetimeDef(name.unwrap()),
DefPathData::EnumVariant => hir_map::DefPathData::EnumVariant(name.unwrap()),
DefPathData::Field => hir_map::DefPathData::Field(name.unwrap()),
DefPathData::StructCtor => hir_map::DefPathData::StructCtor,
DefPathData::Initializer => hir_map::DefPathData::Initializer,
DefPathData::Binding => hir_map::DefPathData::Binding(name.unwrap()),
}
}
22 changes: 11 additions & 11 deletions src/librustc_metadata/encoder.rs
Expand Up @@ -17,6 +17,7 @@ use astencode::encode_inlined_item;
use common::*;
use cstore;
use decoder;
use def_key;
use tyencode;
use index::{self, IndexData};

Expand All @@ -35,7 +36,7 @@ use rustc::mir::mir_map::MirMap;
use rustc::session::config;
use rustc::util::nodemap::{FnvHashMap, NodeMap, NodeSet};

use serialize::Encodable;
use rustc_serialize::Encodable;
use std::cell::RefCell;
use std::io::prelude::*;
use std::io::{Cursor, SeekFrom};
Expand All @@ -53,6 +54,7 @@ use rbml::writer::Encoder;
use rustc::hir::{self, PatKind};
use rustc::hir::intravisit::Visitor;
use rustc::hir::intravisit;
use rustc::hir::map::DefKey;

pub struct EncodeContext<'a, 'tcx: 'a> {
pub diag: &'a Handler,
Expand Down Expand Up @@ -101,24 +103,22 @@ fn encode_def_id(rbml_w: &mut Encoder, id: DefId) {
rbml_w.wr_tagged_u64(tag_def_id, def_to_u64(id));
}

fn encode_def_key(rbml_w: &mut Encoder, key: DefKey) {
let simple_key = def_key::simplify_def_key(key);
rbml_w.start_tag(tag_def_key);
simple_key.encode(rbml_w);
rbml_w.end_tag();
}

/// For every DefId that we create a metadata item for, we include a
/// serialized copy of its DefKey, which allows us to recreate a path.
fn encode_def_id_and_key(ecx: &EncodeContext,
rbml_w: &mut Encoder,
def_id: DefId)
{
encode_def_id(rbml_w, def_id);
encode_def_key(ecx, rbml_w, def_id);
}

fn encode_def_key(ecx: &EncodeContext,
rbml_w: &mut Encoder,
def_id: DefId)
{
rbml_w.start_tag(tag_def_key);
let def_key = ecx.tcx.map.def_key(def_id);
def_key.encode(rbml_w);
rbml_w.end_tag();
encode_def_key(rbml_w, def_key);
}

fn encode_trait_ref<'a, 'tcx>(rbml_w: &mut Encoder,
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_metadata/lib.rs
Expand Up @@ -31,7 +31,7 @@

extern crate flate;
extern crate rbml;
extern crate serialize;
extern crate serialize as rustc_serialize; // used by deriving

#[macro_use]
extern crate rustc;
Expand All @@ -48,6 +48,7 @@ pub mod diagnostics;

pub mod astencode;
pub mod common;
pub mod def_key;
pub mod tyencode;
pub mod tydecode;
pub mod encoder;
Expand Down

0 comments on commit 2fd1f3e

Please sign in to comment.