1use rls_span as span;
2
3use std::path::PathBuf;
4
5#[cfg(feature = "derive")]
6use serde::{Deserialize, Serialize};
7
8pub mod config;
9pub use config::Config;
10
11#[derive(Debug, Clone, Default)]
12#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
13#[repr(C)]
14pub struct Analysis {
15 pub config: Config,
17 pub version: Option<String>,
18 pub compilation: Option<CompilationOptions>,
19 pub prelude: Option<CratePreludeData>,
20 pub imports: Vec<Import>,
21 pub defs: Vec<Def>,
22 pub impls: Vec<Impl>,
23 pub refs: Vec<Ref>,
24 pub macro_refs: Vec<MacroRef>,
25 pub relations: Vec<Relation>,
26}
27
28impl Analysis {
29 pub fn new(config: Config) -> Analysis {
32 Analysis {
33 config,
34 version: option_env!("CARGO_PKG_VERSION").map(ToString::to_string),
35 ..Analysis::default()
36 }
37 }
38}
39
40#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
43#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
44pub struct Id {
45 pub krate: u32,
46 pub index: u32,
47}
48
49#[derive(Debug, Clone, PartialEq, Eq, Hash)]
54#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
55pub struct GlobalCrateId {
56 pub name: String,
57 pub disambiguator: (u64, u64),
58}
59
60#[derive(Debug, Clone)]
61#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
62pub struct SpanData {
63 pub file_name: PathBuf,
64 pub byte_start: u32,
65 pub byte_end: u32,
66 pub line_start: span::Row<span::OneIndexed>,
67 pub line_end: span::Row<span::OneIndexed>,
68 pub column_start: span::Column<span::OneIndexed>,
70 pub column_end: span::Column<span::OneIndexed>,
71}
72
73#[derive(Debug, Clone)]
74#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
75pub struct CompilationOptions {
76 pub directory: PathBuf,
77 pub program: String,
78 pub arguments: Vec<String>,
79 pub output: PathBuf,
80}
81
82#[derive(Debug, Clone)]
83#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
84pub struct CratePreludeData {
85 pub crate_id: GlobalCrateId,
86 pub crate_root: String,
87 pub external_crates: Vec<ExternalCrateData>,
88 pub span: SpanData,
89}
90
91#[derive(Debug, Clone)]
93#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
94pub struct ExternalCrateData {
95 pub file_name: String,
97 pub num: u32,
101 pub id: GlobalCrateId,
102}
103
104#[derive(Debug, Clone)]
105#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
106pub struct Import {
107 pub kind: ImportKind,
108 pub ref_id: Option<Id>,
109 pub span: SpanData,
110 pub alias_span: Option<SpanData>,
111 pub name: String,
112 pub value: String,
113 pub parent: Option<Id>,
114}
115
116#[derive(Debug, Clone, Copy, PartialEq, Eq)]
117#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
118pub enum ImportKind {
119 ExternCrate,
120 Use,
121 GlobUse,
122}
123
124#[derive(Debug, Clone)]
125#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
126pub struct Def {
127 pub kind: DefKind,
128 pub id: Id,
129 pub span: SpanData,
130 pub name: String,
131 pub qualname: String,
132 pub value: String,
133 pub parent: Option<Id>,
134 pub children: Vec<Id>,
135 pub decl_id: Option<Id>,
136 pub docs: String,
137 pub sig: Option<Signature>,
138 pub attributes: Vec<Attribute>,
139}
140
141#[derive(Debug, Clone, Copy, PartialEq, Eq)]
142#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
143pub enum DefKind {
144 Enum,
146 TupleVariant,
148 StructVariant,
150 Tuple,
152 Struct,
154 Union,
155 Trait,
157 Function,
159 ForeignFunction,
160 Method,
162 Macro,
164 Mod,
166 Type,
168 Local,
170 Static,
171 ForeignStatic,
172 Const,
173 Field,
174 ExternType,
176}
177
178#[derive(Debug, Clone)]
179#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
180pub struct Impl {
181 pub id: u32,
182 pub kind: ImplKind,
183 pub span: SpanData,
184 pub value: String,
185 pub parent: Option<Id>,
186 pub children: Vec<Id>,
187 pub docs: String,
188 pub sig: Option<Signature>,
189 pub attributes: Vec<Attribute>,
190}
191
192#[derive(Debug, Clone, PartialEq, Eq)]
193#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
194pub enum ImplKind {
195 Inherent,
197 Direct,
199 Indirect,
201 Blanket,
204 Deref(String, Id),
208}
209
210#[derive(Debug, Clone)]
211#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
212pub struct Attribute {
213 pub value: String,
214 pub span: SpanData,
215}
216
217#[derive(Debug, Clone)]
218#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
219pub struct Ref {
220 pub kind: RefKind,
221 pub span: SpanData,
222 pub ref_id: Id,
223}
224
225#[derive(Debug, Clone, Copy, PartialEq, Eq)]
226#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
227pub enum RefKind {
228 Function,
229 Mod,
230 Type,
231 Variable,
232}
233
234#[derive(Debug, Clone)]
235#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
236pub struct MacroRef {
237 pub span: SpanData,
238 pub qualname: String,
239 pub callee_span: SpanData,
240}
241
242#[derive(Debug, Clone)]
243#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
244pub struct Relation {
245 pub span: SpanData,
246 pub kind: RelationKind,
247 pub from: Id,
248 pub to: Id,
249}
250
251#[derive(Debug, Clone, Copy, PartialEq, Eq)]
252#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
253pub enum RelationKind {
254 Impl { id: u32 },
255 SuperTrait,
256}
257
258#[derive(Debug, Clone)]
259#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
260pub struct Signature {
261 pub text: String,
262 pub defs: Vec<SigElement>,
263 pub refs: Vec<SigElement>,
264}
265
266#[derive(Debug, Clone)]
267#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
268pub struct SigElement {
269 pub id: Id,
270 pub start: usize,
271 pub end: usize,
272}