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