]>
Commit | Line | Data |
---|---|---|
cc61c64b XL |
1 | // Copyright 2017 The Rust Project Developers. See the COPYRIGHT |
2 | // at http://rust-lang.org/COPYRIGHT. | |
3 | // | |
4 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
5 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
6 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
7 | // option. This file may not be copied, modified, or distributed | |
8 | // except according to those terms. | |
9 | ||
10 | #![cfg_attr(rustbuild, feature(staged_api, rustc_private))] | |
11 | #![cfg_attr(rustbuild, unstable(feature = "rustc_private", issue = "27812"))] | |
12 | ||
13 | extern crate rustc_serialize; | |
14 | extern crate rls_span as span; | |
15 | ||
3b2f2976 XL |
16 | #[cfg(feature = "serialize-serde")] |
17 | extern crate serde; | |
18 | #[cfg(feature = "serialize-serde")] | |
19 | #[macro_use] | |
20 | extern crate serde_derive; | |
21 | ||
22 | pub mod config; | |
23 | ||
cc61c64b XL |
24 | use std::path::PathBuf; |
25 | ||
3b2f2976 | 26 | use config::Config; |
cc61c64b | 27 | |
cc61c64b XL |
28 | |
29 | #[derive(Debug, Clone, RustcDecodable, RustcEncodable)] | |
7cac9316 | 30 | #[repr(C)] |
cc61c64b | 31 | pub struct Analysis { |
3b2f2976 XL |
32 | /// The Config used to generate this analysis data. |
33 | pub config: Config, | |
cc61c64b XL |
34 | pub prelude: Option<CratePreludeData>, |
35 | pub imports: Vec<Import>, | |
36 | pub defs: Vec<Def>, | |
041b39d2 | 37 | pub impls: Vec<Impl>, |
cc61c64b XL |
38 | pub refs: Vec<Ref>, |
39 | pub macro_refs: Vec<MacroRef>, | |
40 | pub relations: Vec<Relation>, | |
041b39d2 XL |
41 | #[cfg(feature = "borrows")] |
42 | pub per_fn_borrows: Vec<BorrowData>, | |
cc61c64b XL |
43 | } |
44 | ||
45 | impl Analysis { | |
041b39d2 | 46 | #[cfg(not(feature = "borrows"))] |
3b2f2976 | 47 | pub fn new(config: Config) -> Analysis { |
cc61c64b | 48 | Analysis { |
3b2f2976 | 49 | config, |
cc61c64b XL |
50 | prelude: None, |
51 | imports: vec![], | |
52 | defs: vec![], | |
041b39d2 | 53 | impls: vec![], |
cc61c64b XL |
54 | refs: vec![], |
55 | macro_refs: vec![], | |
56 | relations: vec![], | |
57 | } | |
58 | } | |
041b39d2 XL |
59 | |
60 | #[cfg(feature = "borrows")] | |
3b2f2976 | 61 | pub fn new(config: Config) -> Analysis { |
041b39d2 | 62 | Analysis { |
3b2f2976 | 63 | config, |
041b39d2 XL |
64 | prelude: None, |
65 | imports: vec![], | |
66 | defs: vec![], | |
67 | impls: vec![], | |
68 | refs: vec![], | |
69 | macro_refs: vec![], | |
70 | relations: vec![], | |
71 | per_fn_borrows: vec![], | |
72 | } | |
73 | } | |
cc61c64b XL |
74 | } |
75 | ||
76 | // DefId::index is a newtype and so the JSON serialisation is ugly. Therefore | |
77 | // we use our own Id which is the same, but without the newtype. | |
3b2f2976 | 78 | #[derive(Clone, Copy, Debug, RustcDecodable, RustcEncodable, PartialEq, Eq, Hash)] |
cc61c64b XL |
79 | pub struct Id { |
80 | pub krate: u32, | |
81 | pub index: u32, | |
82 | } | |
83 | ||
abe05a73 XL |
84 | /// Crate name, along with its disambiguator (128-bit hash) represents a globally |
85 | /// unique crate identifier, which should allow for differentiation between | |
86 | /// different crate targets or versions and should point to the same crate when | |
87 | /// pulled by different other, dependent crates. | |
88 | #[derive(Debug, Clone, RustcDecodable, RustcEncodable, PartialEq, Eq, Hash)] | |
89 | pub struct GlobalCrateId { | |
90 | pub name: String, | |
91 | pub disambiguator: (u64, u64), | |
92 | } | |
93 | ||
cc61c64b XL |
94 | #[derive(Debug, Clone, RustcDecodable, RustcEncodable)] |
95 | pub struct SpanData { | |
96 | pub file_name: PathBuf, | |
97 | pub byte_start: u32, | |
98 | pub byte_end: u32, | |
99 | pub line_start: span::Row<span::OneIndexed>, | |
100 | pub line_end: span::Row<span::OneIndexed>, | |
101 | // Character offset. | |
102 | pub column_start: span::Column<span::OneIndexed>, | |
103 | pub column_end: span::Column<span::OneIndexed>, | |
104 | } | |
105 | ||
106 | #[derive(Debug, Clone, RustcDecodable, RustcEncodable)] | |
107 | pub struct CratePreludeData { | |
abe05a73 | 108 | pub crate_id: GlobalCrateId, |
cc61c64b XL |
109 | pub crate_root: String, |
110 | pub external_crates: Vec<ExternalCrateData>, | |
111 | pub span: SpanData, | |
112 | } | |
113 | ||
114 | /// Data for external crates in the prelude of a crate. | |
115 | #[derive(Debug, Clone, RustcDecodable, RustcEncodable)] | |
116 | pub struct ExternalCrateData { | |
abe05a73 | 117 | /// Source file where the external crate is declared. |
cc61c64b | 118 | pub file_name: String, |
abe05a73 XL |
119 | /// A crate-local crate index of an external crate. Local crate index is |
120 | /// always 0, so these should start from 1 and range should be contiguous, | |
121 | /// e.g. from 1 to n for n external crates. | |
122 | pub num: u32, | |
123 | pub id: GlobalCrateId, | |
cc61c64b XL |
124 | } |
125 | ||
126 | #[derive(Debug, Clone, RustcDecodable, RustcEncodable)] | |
127 | pub struct Import { | |
128 | pub kind: ImportKind, | |
129 | pub ref_id: Option<Id>, | |
130 | pub span: SpanData, | |
131 | pub name: String, | |
132 | pub value: String, | |
ff7c6d11 | 133 | pub parent: Option<Id>, |
cc61c64b XL |
134 | } |
135 | ||
136 | #[derive(Debug, RustcDecodable, RustcEncodable, Clone, Copy, PartialEq, Eq)] | |
137 | pub enum ImportKind { | |
138 | ExternCrate, | |
139 | Use, | |
140 | GlobUse, | |
141 | } | |
142 | ||
143 | #[derive(Debug, Clone, RustcDecodable, RustcEncodable)] | |
144 | pub struct Def { | |
145 | pub kind: DefKind, | |
146 | pub id: Id, | |
147 | pub span: SpanData, | |
148 | pub name: String, | |
149 | pub qualname: String, | |
150 | pub value: String, | |
151 | pub parent: Option<Id>, | |
152 | pub children: Vec<Id>, | |
153 | pub decl_id: Option<Id>, | |
154 | pub docs: String, | |
155 | pub sig: Option<Signature>, | |
156 | pub attributes: Vec<Attribute>, | |
157 | } | |
158 | ||
159 | #[derive(Debug, RustcDecodable, RustcEncodable, Clone, Copy, PartialEq, Eq)] | |
160 | pub enum DefKind { | |
161 | // value = variant names | |
162 | Enum, | |
163 | // value = enum name + variant name + types | |
abe05a73 XL |
164 | TupleVariant, |
165 | // value = enum name + name + fields | |
166 | StructVariant, | |
167 | // value = variant name + types | |
cc61c64b | 168 | Tuple, |
abe05a73 | 169 | // value = name + fields |
cc61c64b XL |
170 | Struct, |
171 | Union, | |
172 | // value = signature | |
173 | Trait, | |
174 | // value = type + generics | |
175 | Function, | |
176 | // value = type + generics | |
177 | Method, | |
178 | // No id, no value. | |
179 | Macro, | |
180 | // value = file_name | |
181 | Mod, | |
182 | // value = aliased type | |
183 | Type, | |
184 | // value = type and init expression (for all variable kinds). | |
185 | Local, | |
186 | Static, | |
187 | Const, | |
188 | Field, | |
abe05a73 XL |
189 | // no value |
190 | ExternType, | |
cc61c64b XL |
191 | } |
192 | ||
041b39d2 XL |
193 | #[derive(Debug, Clone, RustcDecodable, RustcEncodable)] |
194 | pub struct Impl { | |
195 | pub id: u32, | |
196 | pub kind: ImplKind, | |
197 | pub span: SpanData, | |
198 | pub value: String, | |
199 | pub parent: Option<Id>, | |
200 | pub children: Vec<Id>, | |
201 | pub docs: String, | |
202 | pub sig: Option<Signature>, | |
203 | pub attributes: Vec<Attribute>, | |
204 | } | |
205 | ||
206 | #[derive(Debug, RustcDecodable, RustcEncodable, Clone, PartialEq, Eq)] | |
207 | pub enum ImplKind { | |
208 | // impl Foo { ... } | |
209 | Inherent, | |
210 | // impl Bar for Foo { ... } | |
211 | Direct, | |
212 | // impl Bar for &Foo { ... } | |
213 | Indirect, | |
214 | // impl<T: Baz> Bar for T { ... } | |
215 | // where Foo: Baz | |
216 | Blanket, | |
217 | // impl Bar for Baz { ... } or impl Baz { ... }, etc. | |
218 | // where Foo: Deref<Target = Baz> | |
219 | // Args are name and id of Baz | |
220 | Deref(String, Id), | |
221 | } | |
222 | ||
cc61c64b XL |
223 | #[derive(Debug, Clone, RustcDecodable, RustcEncodable)] |
224 | pub struct Attribute { | |
225 | pub value: String, | |
226 | pub span: SpanData, | |
227 | } | |
228 | ||
229 | #[derive(Debug, Clone, RustcDecodable, RustcEncodable)] | |
230 | pub struct Ref { | |
231 | pub kind: RefKind, | |
232 | pub span: SpanData, | |
233 | pub ref_id: Id, | |
234 | } | |
235 | ||
236 | #[derive(Debug, RustcDecodable, RustcEncodable, Clone, Copy, PartialEq, Eq)] | |
237 | pub enum RefKind { | |
238 | Function, | |
239 | Mod, | |
240 | Type, | |
241 | Variable, | |
242 | } | |
243 | ||
cc61c64b XL |
244 | #[derive(Debug, Clone, RustcDecodable, RustcEncodable)] |
245 | pub struct MacroRef { | |
246 | pub span: SpanData, | |
247 | pub qualname: String, | |
248 | pub callee_span: SpanData, | |
249 | } | |
250 | ||
251 | #[derive(Debug, Clone, RustcDecodable, RustcEncodable)] | |
252 | pub struct Relation { | |
253 | pub span: SpanData, | |
254 | pub kind: RelationKind, | |
255 | pub from: Id, | |
256 | pub to: Id, | |
257 | } | |
258 | ||
259 | #[derive(Debug, RustcDecodable, RustcEncodable, Clone, Copy, PartialEq, Eq)] | |
260 | pub enum RelationKind { | |
2c00a5a8 XL |
261 | Impl { |
262 | id: u32, | |
263 | }, | |
cc61c64b XL |
264 | SuperTrait, |
265 | } | |
266 | ||
267 | #[derive(Debug, Clone, RustcDecodable, RustcEncodable)] | |
268 | pub struct Signature { | |
cc61c64b | 269 | pub text: String, |
cc61c64b XL |
270 | pub defs: Vec<SigElement>, |
271 | pub refs: Vec<SigElement>, | |
272 | } | |
273 | ||
274 | #[derive(Debug, Clone, RustcDecodable, RustcEncodable)] | |
275 | pub struct SigElement { | |
276 | pub id: Id, | |
277 | pub start: usize, | |
278 | pub end: usize, | |
279 | } | |
041b39d2 XL |
280 | |
281 | // Each `BorrowData` represents all of the scopes, loans and moves | |
282 | // within an fn or closure referred to by `ref_id`. | |
283 | #[cfg(feature = "borrows")] | |
284 | #[derive(Debug, Clone, RustcDecodable, RustcEncodable)] | |
285 | pub struct BorrowData { | |
286 | pub ref_id: Id, | |
287 | pub scopes: Vec<Scope>, | |
288 | pub loans: Vec<Loan>, | |
289 | pub moves: Vec<Move>, | |
3b2f2976 | 290 | pub span: Option<SpanData>, |
041b39d2 XL |
291 | } |
292 | ||
293 | #[cfg(feature = "borrows")] | |
294 | #[derive(Debug, RustcDecodable, RustcEncodable, Clone, Copy)] | |
295 | pub enum BorrowKind { | |
296 | ImmBorrow, | |
297 | MutBorrow, | |
298 | } | |
299 | ||
300 | // Each `Loan` is either temporary or assigned to a variable. | |
301 | // The `ref_id` refers to the value that is being loaned/borrowed. | |
302 | // Not all loans will be valid. Invalid loans can be used to help explain | |
303 | // improper usage. | |
304 | #[cfg(feature = "borrows")] | |
305 | #[derive(Debug, Clone, RustcDecodable, RustcEncodable)] | |
306 | pub struct Loan { | |
307 | pub ref_id: Id, | |
308 | pub kind: BorrowKind, | |
309 | pub span: SpanData, | |
310 | } | |
311 | ||
312 | // Each `Move` represents an attempt to move the value referred to by `ref_id`. | |
313 | // Not all `Move`s will be valid but can be used to help explain improper usage. | |
314 | #[cfg(feature = "borrows")] | |
315 | #[derive(Debug, Clone, RustcDecodable, RustcEncodable)] | |
316 | pub struct Move { | |
317 | pub ref_id: Id, | |
318 | pub span: SpanData, | |
319 | } | |
320 | ||
321 | // Each `Scope` refers to "scope" of a variable (we don't track all values here). | |
322 | // Its ref_id refers to the variable, and the span refers to the scope/region where | |
323 | // the variable is "live". | |
324 | #[cfg(feature = "borrows")] | |
325 | #[derive(Debug, Clone, RustcDecodable, RustcEncodable)] | |
326 | pub struct Scope { | |
327 | pub ref_id: Id, | |
328 | pub span: SpanData, | |
329 | } |