|
| 1 | +// Copyright 2016 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::def_id::DefIndex; |
| 12 | +use rustc::hir::map as hir_map; |
| 13 | +use syntax::ast::Name; |
| 14 | + |
| 15 | +#[derive(RustcEncodable, RustcDecodable)] |
| 16 | +pub struct DefKey { |
| 17 | + pub parent: Option<DefIndex>, |
| 18 | + pub disambiguated_data: DisambiguatedDefPathData, |
| 19 | +} |
| 20 | + |
| 21 | +#[derive(RustcEncodable, RustcDecodable)] |
| 22 | +pub struct DisambiguatedDefPathData { |
| 23 | + pub data: DefPathData, |
| 24 | + pub disambiguator: u32, |
| 25 | +} |
| 26 | + |
| 27 | +#[derive(RustcEncodable, RustcDecodable)] |
| 28 | +pub enum DefPathData { |
| 29 | + CrateRoot, |
| 30 | + Misc, |
| 31 | + Impl, |
| 32 | + TypeNs, |
| 33 | + ValueNs, |
| 34 | + MacroDef, |
| 35 | + ClosureExpr, |
| 36 | + TypeParam, |
| 37 | + LifetimeDef, |
| 38 | + EnumVariant, |
| 39 | + Field, |
| 40 | + StructCtor, |
| 41 | + Initializer, |
| 42 | + Binding, |
| 43 | +} |
| 44 | + |
| 45 | +pub fn simplify_def_key(key: hir_map::DefKey) -> DefKey { |
| 46 | + let data = DisambiguatedDefPathData { |
| 47 | + data: simplify_def_path_data(key.disambiguated_data.data), |
| 48 | + disambiguator: key.disambiguated_data.disambiguator, |
| 49 | + }; |
| 50 | + DefKey { |
| 51 | + parent: key.parent, |
| 52 | + disambiguated_data: data, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +fn simplify_def_path_data(data: hir_map::DefPathData) -> DefPathData { |
| 57 | + match data { |
| 58 | + hir_map::DefPathData::CrateRoot => DefPathData::CrateRoot, |
| 59 | + hir_map::DefPathData::InlinedRoot(_) => bug!("unexpected DefPathData"), |
| 60 | + hir_map::DefPathData::Misc => DefPathData::Misc, |
| 61 | + hir_map::DefPathData::Impl => DefPathData::Impl, |
| 62 | + hir_map::DefPathData::TypeNs(_) => DefPathData::TypeNs, |
| 63 | + hir_map::DefPathData::ValueNs(_) => DefPathData::ValueNs, |
| 64 | + hir_map::DefPathData::MacroDef(_) => DefPathData::MacroDef, |
| 65 | + hir_map::DefPathData::ClosureExpr => DefPathData::ClosureExpr, |
| 66 | + hir_map::DefPathData::TypeParam(_) => DefPathData::TypeParam, |
| 67 | + hir_map::DefPathData::LifetimeDef(_) => DefPathData::LifetimeDef, |
| 68 | + hir_map::DefPathData::EnumVariant(_) => DefPathData::EnumVariant, |
| 69 | + hir_map::DefPathData::Field(_) => DefPathData::Field, |
| 70 | + hir_map::DefPathData::StructCtor => DefPathData::StructCtor, |
| 71 | + hir_map::DefPathData::Initializer => DefPathData::Initializer, |
| 72 | + hir_map::DefPathData::Binding(_) => DefPathData::Binding, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +pub fn recover_def_key(key: DefKey, name: Option<Name>) -> hir_map::DefKey { |
| 77 | + let data = hir_map::DisambiguatedDefPathData { |
| 78 | + data: recover_def_path_data(key.disambiguated_data.data, name), |
| 79 | + disambiguator: key.disambiguated_data.disambiguator, |
| 80 | + }; |
| 81 | + hir_map::DefKey { |
| 82 | + parent: key.parent, |
| 83 | + disambiguated_data: data, |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +fn recover_def_path_data(data: DefPathData, name: Option<Name>) -> hir_map::DefPathData { |
| 88 | + match data { |
| 89 | + DefPathData::CrateRoot => hir_map::DefPathData::CrateRoot, |
| 90 | + DefPathData::Misc => hir_map::DefPathData::Misc, |
| 91 | + DefPathData::Impl => hir_map::DefPathData::Impl, |
| 92 | + DefPathData::TypeNs => hir_map::DefPathData::TypeNs(name.unwrap()), |
| 93 | + DefPathData::ValueNs => hir_map::DefPathData::ValueNs(name.unwrap()), |
| 94 | + DefPathData::MacroDef => hir_map::DefPathData::MacroDef(name.unwrap()), |
| 95 | + DefPathData::ClosureExpr => hir_map::DefPathData::ClosureExpr, |
| 96 | + DefPathData::TypeParam => hir_map::DefPathData::TypeParam(name.unwrap()), |
| 97 | + DefPathData::LifetimeDef => hir_map::DefPathData::LifetimeDef(name.unwrap()), |
| 98 | + DefPathData::EnumVariant => hir_map::DefPathData::EnumVariant(name.unwrap()), |
| 99 | + DefPathData::Field => hir_map::DefPathData::Field(name.unwrap()), |
| 100 | + DefPathData::StructCtor => hir_map::DefPathData::StructCtor, |
| 101 | + DefPathData::Initializer => hir_map::DefPathData::Initializer, |
| 102 | + DefPathData::Binding => hir_map::DefPathData::Binding(name.unwrap()), |
| 103 | + } |
| 104 | +} |
0 commit comments