]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/doctree.rs
Merge commit '3e7c6dec244539970b593824334876f8b6ed0b18' into clippyup
[rust.git] / src / librustdoc / doctree.rs
1 //! This module is used to store stuff from Rust's AST in a more convenient
2 //! manner (and with prettier names) before cleaning.
3 crate use self::StructType::*;
4
5 use rustc_ast as ast;
6 use rustc_span::hygiene::MacroKind;
7 use rustc_span::{self, symbol::Ident, Span, Symbol};
8
9 use rustc_hir as hir;
10 use rustc_hir::def_id::CrateNum;
11 use rustc_hir::HirId;
12
13 crate struct Module<'hir> {
14     crate name: Option<Symbol>,
15     crate attrs: &'hir [ast::Attribute],
16     crate where_outer: Span,
17     crate where_inner: Span,
18     crate extern_crates: Vec<ExternCrate<'hir>>,
19     crate imports: Vec<Import<'hir>>,
20     crate fns: Vec<Function<'hir>>,
21     crate mods: Vec<Module<'hir>>,
22     crate id: hir::HirId,
23     // (item, renamed)
24     crate items: Vec<(&'hir hir::Item<'hir>, Option<Ident>)>,
25     crate traits: Vec<Trait<'hir>>,
26     crate impls: Vec<Impl<'hir>>,
27     crate foreigns: Vec<ForeignItem<'hir>>,
28     crate macros: Vec<Macro>,
29     crate proc_macros: Vec<ProcMacro>,
30     crate is_crate: bool,
31 }
32
33 impl Module<'hir> {
34     crate fn new(name: Option<Symbol>, attrs: &'hir [ast::Attribute]) -> Module<'hir> {
35         Module {
36             name,
37             id: hir::CRATE_HIR_ID,
38             where_outer: rustc_span::DUMMY_SP,
39             where_inner: rustc_span::DUMMY_SP,
40             attrs,
41             extern_crates: Vec::new(),
42             imports: Vec::new(),
43             fns: Vec::new(),
44             mods: Vec::new(),
45             items: Vec::new(),
46             traits: Vec::new(),
47             impls: Vec::new(),
48             foreigns: Vec::new(),
49             macros: Vec::new(),
50             proc_macros: Vec::new(),
51             is_crate: false,
52         }
53     }
54 }
55
56 #[derive(Debug, Clone, Copy)]
57 crate enum StructType {
58     /// A braced struct
59     Plain,
60     /// A tuple struct
61     Tuple,
62     /// A unit struct
63     Unit,
64 }
65
66 crate struct Variant<'hir> {
67     crate name: Symbol,
68     crate id: hir::HirId,
69     crate def: &'hir hir::VariantData<'hir>,
70 }
71
72 crate struct Function<'hir> {
73     crate decl: &'hir hir::FnDecl<'hir>,
74     crate id: hir::HirId,
75     crate name: Symbol,
76     crate header: hir::FnHeader,
77     crate generics: &'hir hir::Generics<'hir>,
78     crate body: hir::BodyId,
79 }
80
81 crate struct Trait<'hir> {
82     crate is_auto: hir::IsAuto,
83     crate unsafety: hir::Unsafety,
84     crate name: Symbol,
85     crate items: Vec<&'hir hir::TraitItem<'hir>>,
86     crate generics: &'hir hir::Generics<'hir>,
87     crate bounds: &'hir [hir::GenericBound<'hir>],
88     crate attrs: &'hir [ast::Attribute],
89     crate id: hir::HirId,
90 }
91
92 #[derive(Debug)]
93 crate struct Impl<'hir> {
94     crate unsafety: hir::Unsafety,
95     crate polarity: hir::ImplPolarity,
96     crate defaultness: hir::Defaultness,
97     crate constness: hir::Constness,
98     crate generics: &'hir hir::Generics<'hir>,
99     crate trait_: &'hir Option<hir::TraitRef<'hir>>,
100     crate for_: &'hir hir::Ty<'hir>,
101     crate items: Vec<&'hir hir::ImplItem<'hir>>,
102     crate attrs: &'hir [ast::Attribute],
103     crate span: Span,
104     crate vis: &'hir hir::Visibility<'hir>,
105     crate id: hir::HirId,
106 }
107
108 crate struct ForeignItem<'hir> {
109     crate id: hir::HirId,
110     crate name: Symbol,
111     crate kind: &'hir hir::ForeignItemKind<'hir>,
112 }
113
114 // For Macro we store the DefId instead of the NodeId, since we also create
115 // these imported macro_rules (which only have a DUMMY_NODE_ID).
116 crate struct Macro {
117     crate name: Symbol,
118     crate def_id: hir::def_id::DefId,
119     crate matchers: Vec<Span>,
120     crate imported_from: Option<Symbol>,
121 }
122
123 crate struct ExternCrate<'hir> {
124     crate name: Symbol,
125     crate hir_id: HirId,
126     crate cnum: CrateNum,
127     crate path: Option<String>,
128     crate vis: &'hir hir::Visibility<'hir>,
129     crate attrs: &'hir [ast::Attribute],
130     crate span: Span,
131 }
132
133 #[derive(Debug)]
134 crate struct Import<'hir> {
135     crate name: Symbol,
136     crate id: hir::HirId,
137     crate vis: &'hir hir::Visibility<'hir>,
138     crate attrs: &'hir [ast::Attribute],
139     crate path: &'hir hir::Path<'hir>,
140     crate glob: bool,
141     crate span: Span,
142 }
143
144 crate struct ProcMacro {
145     crate name: Symbol,
146     crate id: hir::HirId,
147     crate kind: MacroKind,
148     crate helpers: Vec<Symbol>,
149 }
150
151 crate fn struct_type_from_def(vdata: &hir::VariantData<'_>) -> StructType {
152     match *vdata {
153         hir::VariantData::Struct(..) => Plain,
154         hir::VariantData::Tuple(..) => Tuple,
155         hir::VariantData::Unit(..) => Unit,
156     }
157 }