summary refs log tree commit diff
path: root/src/librustc_metadata/def_key.rs
blob: 05ad333ed3adcf9513e9340cfcc4820920325a7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// 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,
    Module,
    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::Module(_) => DefPathData::Module,
        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::Module => hir_map::DefPathData::Module(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()),
    }
}