├── .gitignore ├── README.md ├── Cargo.toml ├── src ├── config.rs └── lib.rs └── Cargo.lock /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | **/*.rs.bk 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RLS-data 2 | **NOTE:** Development has been moved to the central [RLS](https://github.com/rust-lang/rls) repository. 3 | 4 | Data structures used by the RLS and the Rust compiler. 5 | 6 | These are used by the save-analysis functionality in the compiler 7 | (`rustc -Zsave-analysis`). In that use, the compiler translates info in its 8 | internal data structures to these data structures then serialises them as JSON. 9 | Clients (such as the RLS) can use this crate when deserialising. 10 | 11 | The data can also be passed directly from compiler to client if the compiler is 12 | used as a library. 13 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rls-data" 3 | version = "0.18.2" 4 | authors = ["Nick Cameron "] 5 | description = "Data structures used by the RLS and Rust compiler" 6 | license = "Apache-2.0/MIT" 7 | repository = "https://github.com/rust-dev-tools/rls-data" 8 | categories = ["development-tools"] 9 | 10 | [dependencies] 11 | rls-span = "0.4.1" 12 | rustc-serialize = { version = "0.3", optional = true } 13 | serde = { version = "1.0", optional = true } 14 | serde_derive = { version = "1.0", optional = true } 15 | 16 | [features] 17 | default = ["serialize-rustc"] 18 | borrows=[] 19 | serialize-rustc = ["rustc-serialize", "rls-span/serialize-rustc"] 20 | serialize-serde = ["serde", "serde_derive", "rls-span/serialize-serde"] 21 | -------------------------------------------------------------------------------- /src/config.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 or the MIT license 6 | // , at your 7 | // option. This file may not be copied, modified, or distributed 8 | // except according to those terms. 9 | 10 | /// Used to configure save-analysis. 11 | #[cfg_attr(feature = "serialize-serde", derive(Serialize, Deserialize))] 12 | #[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] 13 | #[derive(Debug, Clone, Default)] 14 | pub struct Config { 15 | /// File to output save-analysis data to. 16 | pub output_file: Option, 17 | /// Include all documentation for items. (If `false`, only includes the 18 | /// summary (first paragraph) for each item). 19 | pub full_docs: bool, 20 | /// If true only includes data for public items in a crate (useful for 21 | /// library crates). 22 | pub pub_only: bool, 23 | /// If true only includes data for items reachable from the crate root. 24 | pub reachable_only: bool, 25 | /// True if and only if the analysed crate is part of the standard Rust distro. 26 | pub distro_crate: bool, 27 | /// Include signature information. 28 | pub signatures: bool, 29 | /// Include experimental borrow data. 30 | pub borrow_data: bool, 31 | } 32 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "proc-macro2" 5 | version = "0.4.26" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | dependencies = [ 8 | "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 9 | ] 10 | 11 | [[package]] 12 | name = "quote" 13 | version = "0.6.11" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | dependencies = [ 16 | "proc-macro2 0.4.26 (registry+https://github.com/rust-lang/crates.io-index)", 17 | ] 18 | 19 | [[package]] 20 | name = "rls-data" 21 | version = "0.18.2" 22 | dependencies = [ 23 | "rls-span 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", 24 | "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", 25 | "serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)", 26 | "serde_derive 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)", 27 | ] 28 | 29 | [[package]] 30 | name = "rls-span" 31 | version = "0.4.1" 32 | source = "registry+https://github.com/rust-lang/crates.io-index" 33 | dependencies = [ 34 | "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", 35 | "serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)", 36 | "serde_derive 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)", 37 | ] 38 | 39 | [[package]] 40 | name = "rustc-serialize" 41 | version = "0.3.24" 42 | source = "registry+https://github.com/rust-lang/crates.io-index" 43 | 44 | [[package]] 45 | name = "serde" 46 | version = "1.0.85" 47 | source = "registry+https://github.com/rust-lang/crates.io-index" 48 | 49 | [[package]] 50 | name = "serde_derive" 51 | version = "1.0.85" 52 | source = "registry+https://github.com/rust-lang/crates.io-index" 53 | dependencies = [ 54 | "proc-macro2 0.4.26 (registry+https://github.com/rust-lang/crates.io-index)", 55 | "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", 56 | "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", 57 | ] 58 | 59 | [[package]] 60 | name = "syn" 61 | version = "0.15.26" 62 | source = "registry+https://github.com/rust-lang/crates.io-index" 63 | dependencies = [ 64 | "proc-macro2 0.4.26 (registry+https://github.com/rust-lang/crates.io-index)", 65 | "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", 66 | "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 67 | ] 68 | 69 | [[package]] 70 | name = "unicode-xid" 71 | version = "0.1.0" 72 | source = "registry+https://github.com/rust-lang/crates.io-index" 73 | 74 | [metadata] 75 | "checksum proc-macro2 0.4.26 (registry+https://github.com/rust-lang/crates.io-index)" = "38fddd23d98b2144d197c0eca5705632d4fe2667d14a6be5df8934f8d74f1978" 76 | "checksum quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "cdd8e04bd9c52e0342b406469d494fcb033be4bdbe5c606016defbb1681411e1" 77 | "checksum rls-span 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "33d66f1d6c6ccd5c98029f162544131698f6ebb61d8c697681cac409dcd08805" 78 | "checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" 79 | "checksum serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)" = "534b8b91a95e0f71bca3ed5824752d558da048d4248c91af873b63bd60519752" 80 | "checksum serde_derive 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)" = "a915306b0f1ac5607797697148c223bedeaa36bcc2e28a01441cd638cc6567b4" 81 | "checksum syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)" = "f92e629aa1d9c827b2bb8297046c1ccffc57c99b947a680d3ccff1f136a3bee9" 82 | "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" 83 | -------------------------------------------------------------------------------- /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 or the MIT license 6 | // , 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, 38 | pub compilation: Option, 39 | pub prelude: Option, 40 | pub imports: Vec, 41 | pub defs: Vec, 42 | pub impls: Vec, 43 | pub refs: Vec, 44 | pub macro_refs: Vec, 45 | pub relations: Vec, 46 | #[cfg(feature = "borrows")] 47 | pub per_fn_borrows: Vec, 48 | } 49 | 50 | impl Analysis { 51 | #[cfg(not(feature = "borrows"))] 52 | pub fn new(config: Config) -> Analysis { 53 | Analysis { 54 | config, 55 | version: option_env!("CARGO_PKG_VERSION").map(|s| s.to_string()), 56 | prelude: None, 57 | compilation: None, 58 | imports: vec![], 59 | defs: vec![], 60 | impls: vec![], 61 | refs: vec![], 62 | macro_refs: vec![], 63 | relations: vec![], 64 | } 65 | } 66 | 67 | #[cfg(feature = "borrows")] 68 | pub fn new(config: Config) -> Analysis { 69 | Analysis { 70 | config, 71 | version: option_env!("CARGO_PKG_VERSION").map(|s| s.to_string()), 72 | prelude: None, 73 | imports: vec![], 74 | defs: vec![], 75 | impls: vec![], 76 | refs: vec![], 77 | macro_refs: vec![], 78 | relations: vec![], 79 | per_fn_borrows: vec![], 80 | } 81 | } 82 | } 83 | 84 | // DefId::index is a newtype and so the JSON serialisation is ugly. Therefore 85 | // we use our own Id which is the same, but without the newtype. 86 | #[cfg_attr(feature = "serialize-serde", derive(Serialize, Deserialize))] 87 | #[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] 88 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] 89 | pub struct Id { 90 | pub krate: u32, 91 | pub index: u32, 92 | } 93 | 94 | /// Crate name, along with its disambiguator (128-bit hash) represents a globally 95 | /// unique crate identifier, which should allow for differentiation between 96 | /// different crate targets or versions and should point to the same crate when 97 | /// pulled by different other, dependent crates. 98 | #[cfg_attr(feature = "serialize-serde", derive(Serialize, Deserialize))] 99 | #[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] 100 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] 101 | pub struct GlobalCrateId { 102 | pub name: String, 103 | pub disambiguator: (u64, u64), 104 | } 105 | 106 | #[cfg_attr(feature = "serialize-serde", derive(Serialize, Deserialize))] 107 | #[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] 108 | #[derive(Debug, Clone)] 109 | pub struct SpanData { 110 | pub file_name: PathBuf, 111 | pub byte_start: u32, 112 | pub byte_end: u32, 113 | pub line_start: span::Row, 114 | pub line_end: span::Row, 115 | // Character offset. 116 | pub column_start: span::Column, 117 | pub column_end: span::Column, 118 | } 119 | 120 | #[cfg_attr(feature = "serialize-serde", derive(Serialize, Deserialize))] 121 | #[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] 122 | #[derive(Debug, Clone)] 123 | pub struct CompilationOptions { 124 | pub directory: PathBuf, 125 | pub program: String, 126 | pub arguments: Vec, 127 | pub output: PathBuf, 128 | } 129 | 130 | #[cfg_attr(feature = "serialize-serde", derive(Serialize, Deserialize))] 131 | #[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] 132 | #[derive(Debug, Clone)] 133 | pub struct CratePreludeData { 134 | pub crate_id: GlobalCrateId, 135 | pub crate_root: String, 136 | pub external_crates: Vec, 137 | pub span: SpanData, 138 | } 139 | 140 | /// Data for external crates in the prelude of a crate. 141 | #[cfg_attr(feature = "serialize-serde", derive(Serialize, Deserialize))] 142 | #[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] 143 | #[derive(Debug, Clone)] 144 | pub struct ExternalCrateData { 145 | /// Source file where the external crate is declared. 146 | pub file_name: String, 147 | /// A crate-local crate index of an external crate. Local crate index is 148 | /// always 0, so these should start from 1 and range should be contiguous, 149 | /// e.g. from 1 to n for n external crates. 150 | pub num: u32, 151 | pub id: GlobalCrateId, 152 | } 153 | 154 | #[cfg_attr(feature = "serialize-serde", derive(Serialize, Deserialize))] 155 | #[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] 156 | #[derive(Debug, Clone)] 157 | pub struct Import { 158 | pub kind: ImportKind, 159 | pub ref_id: Option, 160 | pub span: SpanData, 161 | pub alias_span: Option, 162 | pub name: String, 163 | pub value: String, 164 | pub parent: Option, 165 | } 166 | 167 | #[cfg_attr(feature = "serialize-serde", derive(Serialize, Deserialize))] 168 | #[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] 169 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] 170 | pub enum ImportKind { 171 | ExternCrate, 172 | Use, 173 | GlobUse, 174 | } 175 | 176 | #[cfg_attr(feature = "serialize-serde", derive(Serialize, Deserialize))] 177 | #[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] 178 | #[derive(Debug, Clone)] 179 | pub struct Def { 180 | pub kind: DefKind, 181 | pub id: Id, 182 | pub span: SpanData, 183 | pub name: String, 184 | pub qualname: String, 185 | pub value: String, 186 | pub parent: Option, 187 | pub children: Vec, 188 | pub decl_id: Option, 189 | pub docs: String, 190 | pub sig: Option, 191 | pub attributes: Vec, 192 | } 193 | 194 | #[cfg_attr(feature = "serialize-serde", derive(Serialize, Deserialize))] 195 | #[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] 196 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] 197 | pub enum DefKind { 198 | // value = variant names 199 | Enum, 200 | // value = enum name + variant name + types 201 | TupleVariant, 202 | // value = enum name + name + fields 203 | StructVariant, 204 | // value = variant name + types 205 | Tuple, 206 | // value = name + fields 207 | Struct, 208 | Union, 209 | // value = signature 210 | Trait, 211 | // value = type + generics 212 | Function, 213 | ForeignFunction, 214 | // value = type + generics 215 | Method, 216 | // No id, no value. 217 | Macro, 218 | // value = file_name 219 | Mod, 220 | // value = aliased type 221 | Type, 222 | // value = type and init expression (for all variable kinds). 223 | Local, 224 | Static, 225 | ForeignStatic, 226 | Const, 227 | Field, 228 | // no value 229 | ExternType, 230 | } 231 | 232 | #[cfg_attr(feature = "serialize-serde", derive(Serialize, Deserialize))] 233 | #[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] 234 | #[derive(Debug, Clone)] 235 | pub struct Impl { 236 | pub id: u32, 237 | pub kind: ImplKind, 238 | pub span: SpanData, 239 | pub value: String, 240 | pub parent: Option, 241 | pub children: Vec, 242 | pub docs: String, 243 | pub sig: Option, 244 | pub attributes: Vec, 245 | } 246 | 247 | #[cfg_attr(feature = "serialize-serde", derive(Serialize, Deserialize))] 248 | #[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] 249 | #[derive(Debug, Clone, PartialEq, Eq)] 250 | pub enum ImplKind { 251 | // impl Foo { ... } 252 | Inherent, 253 | // impl Bar for Foo { ... } 254 | Direct, 255 | // impl Bar for &Foo { ... } 256 | Indirect, 257 | // impl Bar for T { ... } 258 | // where Foo: Baz 259 | Blanket, 260 | // impl Bar for Baz { ... } or impl Baz { ... }, etc. 261 | // where Foo: Deref 262 | // Args are name and id of Baz 263 | Deref(String, Id), 264 | } 265 | 266 | #[cfg_attr(feature = "serialize-serde", derive(Serialize, Deserialize))] 267 | #[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] 268 | #[derive(Debug, Clone)] 269 | pub struct Attribute { 270 | pub value: String, 271 | pub span: SpanData, 272 | } 273 | 274 | #[cfg_attr(feature = "serialize-serde", derive(Serialize, Deserialize))] 275 | #[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] 276 | #[derive(Debug, Clone)] 277 | pub struct Ref { 278 | pub kind: RefKind, 279 | pub span: SpanData, 280 | pub ref_id: Id, 281 | } 282 | 283 | #[cfg_attr(feature = "serialize-serde", derive(Serialize, Deserialize))] 284 | #[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] 285 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] 286 | pub enum RefKind { 287 | Function, 288 | Mod, 289 | Type, 290 | Variable, 291 | } 292 | 293 | #[cfg_attr(feature = "serialize-serde", derive(Serialize, Deserialize))] 294 | #[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] 295 | #[derive(Debug, Clone)] 296 | pub struct MacroRef { 297 | pub span: SpanData, 298 | pub qualname: String, 299 | pub callee_span: SpanData, 300 | } 301 | 302 | #[cfg_attr(feature = "serialize-serde", derive(Serialize, Deserialize))] 303 | #[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] 304 | #[derive(Debug, Clone)] 305 | pub struct Relation { 306 | pub span: SpanData, 307 | pub kind: RelationKind, 308 | pub from: Id, 309 | pub to: Id, 310 | } 311 | 312 | #[cfg_attr(feature = "serialize-serde", derive(Serialize, Deserialize))] 313 | #[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] 314 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] 315 | pub enum RelationKind { 316 | Impl { 317 | id: u32, 318 | }, 319 | SuperTrait, 320 | } 321 | 322 | #[cfg_attr(feature = "serialize-serde", derive(Serialize, Deserialize))] 323 | #[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] 324 | #[derive(Debug, Clone)] 325 | pub struct Signature { 326 | pub text: String, 327 | pub defs: Vec, 328 | pub refs: Vec, 329 | } 330 | 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 SigElement { 335 | pub id: Id, 336 | pub start: usize, 337 | pub end: usize, 338 | } 339 | 340 | // Each `BorrowData` represents all of the scopes, loans and moves 341 | // within an fn or closure referred to by `ref_id`. 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)] 346 | pub struct BorrowData { 347 | pub ref_id: Id, 348 | pub scopes: Vec, 349 | pub loans: Vec, 350 | pub moves: Vec, 351 | pub span: Option, 352 | } 353 | 354 | #[cfg(feature = "borrows")] 355 | #[cfg_attr(feature = "serialize-serde", derive(Serialize, Deserialize))] 356 | #[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] 357 | #[derive(Debug, Clone, Copy)] 358 | pub enum BorrowKind { 359 | ImmBorrow, 360 | MutBorrow, 361 | } 362 | 363 | // Each `Loan` is either temporary or assigned to a variable. 364 | // The `ref_id` refers to the value that is being loaned/borrowed. 365 | // Not all loans will be valid. Invalid loans can be used to help explain 366 | // 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 Loan { 372 | pub ref_id: Id, 373 | pub kind: BorrowKind, 374 | pub span: SpanData, 375 | } 376 | 377 | // Each `Move` represents an attempt to move the value referred to by `ref_id`. 378 | // Not all `Move`s will be valid but can be used to help explain improper usage. 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 Move { 384 | pub ref_id: Id, 385 | pub span: SpanData, 386 | } 387 | 388 | // Each `Scope` refers to "scope" of a variable (we don't track all values here). 389 | // Its ref_id refers to the variable, and the span refers to the scope/region where 390 | // the variable is "live". 391 | #[cfg(feature = "borrows")] 392 | #[cfg_attr(feature = "serialize-serde", derive(Serialize, Deserialize))] 393 | #[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] 394 | #[derive(Debug, Clone)] 395 | pub struct Scope { 396 | pub ref_id: Id, 397 | pub span: SpanData, 398 | } 399 | --------------------------------------------------------------------------------