]>
Commit | Line | Data |
---|---|---|
a7813a04 XL |
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; | |
5bcae85e | 13 | use syntax::parse::token::InternedString; |
a7813a04 XL |
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 | Module, | |
35 | MacroDef, | |
36 | ClosureExpr, | |
37 | TypeParam, | |
38 | LifetimeDef, | |
39 | EnumVariant, | |
40 | Field, | |
41 | StructCtor, | |
42 | Initializer, | |
43 | Binding, | |
5bcae85e | 44 | ImplTrait, |
a7813a04 XL |
45 | } |
46 | ||
47 | pub fn simplify_def_key(key: hir_map::DefKey) -> DefKey { | |
48 | let data = DisambiguatedDefPathData { | |
49 | data: simplify_def_path_data(key.disambiguated_data.data), | |
50 | disambiguator: key.disambiguated_data.disambiguator, | |
51 | }; | |
52 | DefKey { | |
53 | parent: key.parent, | |
54 | disambiguated_data: data, | |
55 | } | |
56 | } | |
57 | ||
58 | fn simplify_def_path_data(data: hir_map::DefPathData) -> DefPathData { | |
59 | match data { | |
60 | hir_map::DefPathData::CrateRoot => DefPathData::CrateRoot, | |
61 | hir_map::DefPathData::InlinedRoot(_) => bug!("unexpected DefPathData"), | |
62 | hir_map::DefPathData::Misc => DefPathData::Misc, | |
63 | hir_map::DefPathData::Impl => DefPathData::Impl, | |
64 | hir_map::DefPathData::TypeNs(_) => DefPathData::TypeNs, | |
65 | hir_map::DefPathData::ValueNs(_) => DefPathData::ValueNs, | |
66 | hir_map::DefPathData::Module(_) => DefPathData::Module, | |
67 | hir_map::DefPathData::MacroDef(_) => DefPathData::MacroDef, | |
68 | hir_map::DefPathData::ClosureExpr => DefPathData::ClosureExpr, | |
69 | hir_map::DefPathData::TypeParam(_) => DefPathData::TypeParam, | |
70 | hir_map::DefPathData::LifetimeDef(_) => DefPathData::LifetimeDef, | |
71 | hir_map::DefPathData::EnumVariant(_) => DefPathData::EnumVariant, | |
72 | hir_map::DefPathData::Field(_) => DefPathData::Field, | |
73 | hir_map::DefPathData::StructCtor => DefPathData::StructCtor, | |
74 | hir_map::DefPathData::Initializer => DefPathData::Initializer, | |
75 | hir_map::DefPathData::Binding(_) => DefPathData::Binding, | |
5bcae85e | 76 | hir_map::DefPathData::ImplTrait => DefPathData::ImplTrait, |
a7813a04 XL |
77 | } |
78 | } | |
79 | ||
5bcae85e | 80 | pub fn recover_def_key(key: DefKey, name: Option<InternedString>) -> hir_map::DefKey { |
a7813a04 XL |
81 | let data = hir_map::DisambiguatedDefPathData { |
82 | data: recover_def_path_data(key.disambiguated_data.data, name), | |
83 | disambiguator: key.disambiguated_data.disambiguator, | |
84 | }; | |
85 | hir_map::DefKey { | |
86 | parent: key.parent, | |
87 | disambiguated_data: data, | |
88 | } | |
89 | } | |
90 | ||
5bcae85e | 91 | fn recover_def_path_data(data: DefPathData, name: Option<InternedString>) -> hir_map::DefPathData { |
a7813a04 XL |
92 | match data { |
93 | DefPathData::CrateRoot => hir_map::DefPathData::CrateRoot, | |
94 | DefPathData::Misc => hir_map::DefPathData::Misc, | |
95 | DefPathData::Impl => hir_map::DefPathData::Impl, | |
96 | DefPathData::TypeNs => hir_map::DefPathData::TypeNs(name.unwrap()), | |
97 | DefPathData::ValueNs => hir_map::DefPathData::ValueNs(name.unwrap()), | |
98 | DefPathData::Module => hir_map::DefPathData::Module(name.unwrap()), | |
99 | DefPathData::MacroDef => hir_map::DefPathData::MacroDef(name.unwrap()), | |
100 | DefPathData::ClosureExpr => hir_map::DefPathData::ClosureExpr, | |
101 | DefPathData::TypeParam => hir_map::DefPathData::TypeParam(name.unwrap()), | |
102 | DefPathData::LifetimeDef => hir_map::DefPathData::LifetimeDef(name.unwrap()), | |
103 | DefPathData::EnumVariant => hir_map::DefPathData::EnumVariant(name.unwrap()), | |
104 | DefPathData::Field => hir_map::DefPathData::Field(name.unwrap()), | |
105 | DefPathData::StructCtor => hir_map::DefPathData::StructCtor, | |
106 | DefPathData::Initializer => hir_map::DefPathData::Initializer, | |
107 | DefPathData::Binding => hir_map::DefPathData::Binding(name.unwrap()), | |
5bcae85e | 108 | DefPathData::ImplTrait => hir_map::DefPathData::ImplTrait, |
a7813a04 XL |
109 | } |
110 | } |