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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//! This module is used to store stuff from Rust's AST in a more convenient
//! manner (and with prettier names) before cleaning.
crate use self::StructType::*;

use rustc_ast as ast;
use rustc_span::hygiene::MacroKind;
use rustc_span::{self, Span, Symbol};

use rustc_hir as hir;
use rustc_hir::def_id::CrateNum;
use rustc_hir::HirId;

crate struct Module<'hir> {
    crate name: Option<Symbol>,
    crate attrs: &'hir [ast::Attribute],
    crate where_outer: Span,
    crate where_inner: Span,
    crate extern_crates: Vec<ExternCrate<'hir>>,
    crate imports: Vec<Import<'hir>>,
    crate structs: Vec<Struct<'hir>>,
    crate unions: Vec<Union<'hir>>,
    crate enums: Vec<Enum<'hir>>,
    crate fns: Vec<Function<'hir>>,
    crate mods: Vec<Module<'hir>>,
    crate id: hir::HirId,
    crate typedefs: Vec<Typedef<'hir>>,
    crate opaque_tys: Vec<OpaqueTy<'hir>>,
    crate statics: Vec<Static<'hir>>,
    crate constants: Vec<Constant<'hir>>,
    crate traits: Vec<Trait<'hir>>,
    crate impls: Vec<Impl<'hir>>,
    crate foreigns: Vec<ForeignItem<'hir>>,
    crate macros: Vec<Macro>,
    crate proc_macros: Vec<ProcMacro>,
    crate trait_aliases: Vec<TraitAlias<'hir>>,
    crate is_crate: bool,
}

impl Module<'hir> {
    crate fn new(name: Option<Symbol>, attrs: &'hir [ast::Attribute]) -> Module<'hir> {
        Module {
            name,
            id: hir::CRATE_HIR_ID,
            where_outer: rustc_span::DUMMY_SP,
            where_inner: rustc_span::DUMMY_SP,
            attrs,
            extern_crates: Vec::new(),
            imports: Vec::new(),
            structs: Vec::new(),
            unions: Vec::new(),
            enums: Vec::new(),
            fns: Vec::new(),
            mods: Vec::new(),
            typedefs: Vec::new(),
            opaque_tys: Vec::new(),
            statics: Vec::new(),
            constants: Vec::new(),
            traits: Vec::new(),
            impls: Vec::new(),
            foreigns: Vec::new(),
            macros: Vec::new(),
            proc_macros: Vec::new(),
            trait_aliases: Vec::new(),
            is_crate: false,
        }
    }
}

#[derive(Debug, Clone, Copy)]
crate enum StructType {
    /// A braced struct
    Plain,
    /// A tuple struct
    Tuple,
    /// A unit struct
    Unit,
}

crate struct Struct<'hir> {
    crate id: hir::HirId,
    crate struct_type: StructType,
    crate name: Symbol,
    crate generics: &'hir hir::Generics<'hir>,
    crate fields: &'hir [hir::StructField<'hir>],
}

crate struct Union<'hir> {
    crate id: hir::HirId,
    crate struct_type: StructType,
    crate name: Symbol,
    crate generics: &'hir hir::Generics<'hir>,
    crate fields: &'hir [hir::StructField<'hir>],
}

crate struct Enum<'hir> {
    crate variants: Vec<Variant<'hir>>,
    crate generics: &'hir hir::Generics<'hir>,
    crate id: hir::HirId,
    crate name: Symbol,
}

crate struct Variant<'hir> {
    crate name: Symbol,
    crate id: hir::HirId,
    crate def: &'hir hir::VariantData<'hir>,
}

crate struct Function<'hir> {
    crate decl: &'hir hir::FnDecl<'hir>,
    crate id: hir::HirId,
    crate name: Symbol,
    crate header: hir::FnHeader,
    crate generics: &'hir hir::Generics<'hir>,
    crate body: hir::BodyId,
}

crate struct Typedef<'hir> {
    crate ty: &'hir hir::Ty<'hir>,
    crate gen: &'hir hir::Generics<'hir>,
    crate name: Symbol,
    crate id: hir::HirId,
}

crate struct OpaqueTy<'hir> {
    crate opaque_ty: &'hir hir::OpaqueTy<'hir>,
    crate name: Symbol,
    crate id: hir::HirId,
}

#[derive(Debug)]
crate struct Static<'hir> {
    crate type_: &'hir hir::Ty<'hir>,
    crate mutability: hir::Mutability,
    crate expr: hir::BodyId,
    crate name: Symbol,
    crate attrs: &'hir [ast::Attribute],
    crate vis: &'hir hir::Visibility<'hir>,
    crate id: hir::HirId,
    crate span: Span,
}

crate struct Constant<'hir> {
    crate type_: &'hir hir::Ty<'hir>,
    crate expr: hir::BodyId,
    crate name: Symbol,
    crate id: hir::HirId,
}

crate struct Trait<'hir> {
    crate is_auto: hir::IsAuto,
    crate unsafety: hir::Unsafety,
    crate name: Symbol,
    crate items: Vec<&'hir hir::TraitItem<'hir>>,
    crate generics: &'hir hir::Generics<'hir>,
    crate bounds: &'hir [hir::GenericBound<'hir>],
    crate attrs: &'hir [ast::Attribute],
    crate id: hir::HirId,
}

crate struct TraitAlias<'hir> {
    crate name: Symbol,
    crate generics: &'hir hir::Generics<'hir>,
    crate bounds: &'hir [hir::GenericBound<'hir>],
    crate id: hir::HirId,
}

#[derive(Debug)]
crate struct Impl<'hir> {
    crate unsafety: hir::Unsafety,
    crate polarity: hir::ImplPolarity,
    crate defaultness: hir::Defaultness,
    crate constness: hir::Constness,
    crate generics: &'hir hir::Generics<'hir>,
    crate trait_: &'hir Option<hir::TraitRef<'hir>>,
    crate for_: &'hir hir::Ty<'hir>,
    crate items: Vec<&'hir hir::ImplItem<'hir>>,
    crate attrs: &'hir [ast::Attribute],
    crate span: Span,
    crate vis: &'hir hir::Visibility<'hir>,
    crate id: hir::HirId,
}

crate struct ForeignItem<'hir> {
    crate id: hir::HirId,
    crate name: Symbol,
    crate kind: &'hir hir::ForeignItemKind<'hir>,
}

// For Macro we store the DefId instead of the NodeId, since we also create
// these imported macro_rules (which only have a DUMMY_NODE_ID).
crate struct Macro {
    crate name: Symbol,
    crate def_id: hir::def_id::DefId,
    crate matchers: Vec<Span>,
    crate imported_from: Option<Symbol>,
}

crate struct ExternCrate<'hir> {
    crate name: Symbol,
    crate hir_id: HirId,
    crate cnum: CrateNum,
    crate path: Option<String>,
    crate vis: &'hir hir::Visibility<'hir>,
    crate attrs: &'hir [ast::Attribute],
    crate span: Span,
}

#[derive(Debug)]
crate struct Import<'hir> {
    crate name: Symbol,
    crate id: hir::HirId,
    crate vis: &'hir hir::Visibility<'hir>,
    crate attrs: &'hir [ast::Attribute],
    crate path: &'hir hir::Path<'hir>,
    crate glob: bool,
    crate span: Span,
}

crate struct ProcMacro {
    crate name: Symbol,
    crate id: hir::HirId,
    crate kind: MacroKind,
    crate helpers: Vec<Symbol>,
}

crate fn struct_type_from_def(vdata: &hir::VariantData<'_>) -> StructType {
    match *vdata {
        hir::VariantData::Struct(..) => Plain,
        hir::VariantData::Tuple(..) => Tuple,
        hir::VariantData::Unit(..) => Unit,
    }
}