]> git.proxmox.com Git - rustc.git/blob - src/vendor/rls-data/src/lib.rs
New upstream version 1.25.0+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 extern crate rustc_serialize;
14 extern crate rls_span as span;
15
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
24 use std::path::PathBuf;
25
26 use config::Config;
27
28
29 #[derive(Debug, Clone, RustcDecodable, RustcEncodable)]
30 #[repr(C)]
31 pub struct Analysis {
32 /// The Config used to generate this analysis data.
33 pub config: Config,
34 pub prelude: Option<CratePreludeData>,
35 pub imports: Vec<Import>,
36 pub defs: Vec<Def>,
37 pub impls: Vec<Impl>,
38 pub refs: Vec<Ref>,
39 pub macro_refs: Vec<MacroRef>,
40 pub relations: Vec<Relation>,
41 #[cfg(feature = "borrows")]
42 pub per_fn_borrows: Vec<BorrowData>,
43 }
44
45 impl Analysis {
46 #[cfg(not(feature = "borrows"))]
47 pub fn new(config: Config) -> Analysis {
48 Analysis {
49 config,
50 prelude: None,
51 imports: vec![],
52 defs: vec![],
53 impls: vec![],
54 refs: vec![],
55 macro_refs: vec![],
56 relations: vec![],
57 }
58 }
59
60 #[cfg(feature = "borrows")]
61 pub fn new(config: Config) -> Analysis {
62 Analysis {
63 config,
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 }
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.
78 #[derive(Clone, Copy, Debug, RustcDecodable, RustcEncodable, PartialEq, Eq, Hash)]
79 pub struct Id {
80 pub krate: u32,
81 pub index: u32,
82 }
83
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
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 {
108 pub crate_id: GlobalCrateId,
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 {
117 /// Source file where the external crate is declared.
118 pub file_name: String,
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,
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,
133 pub parent: Option<Id>,
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
164 TupleVariant,
165 // value = enum name + name + fields
166 StructVariant,
167 // value = variant name + types
168 Tuple,
169 // value = name + fields
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,
189 // no value
190 ExternType,
191 }
192
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
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
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 {
261 Impl {
262 id: u32,
263 },
264 SuperTrait,
265 }
266
267 #[derive(Debug, Clone, RustcDecodable, RustcEncodable)]
268 pub struct Signature {
269 pub text: String,
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 }
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>,
290 pub span: Option<SpanData>,
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 }