aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model_api.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/code_model_api.rs')
-rw-r--r--crates/ra_hir/src/code_model_api.rs187
1 files changed, 90 insertions, 97 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index 3ff07bd60..6739627b4 100644
--- a/crates/ra_hir/src/code_model_api.rs
+++ b/crates/ra_hir/src/code_model_api.rs
@@ -5,22 +5,23 @@ use ra_db::{CrateId, FileId};
5use ra_syntax::{ast::self, TreeArc, SyntaxNode}; 5use ra_syntax::{ast::self, TreeArc, SyntaxNode};
6 6
7use crate::{ 7use crate::{
8 Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, Ty, HirFileId, 8 Name, Path, PerNs, ScopesWithSyntaxMapping, Ty, HirFileId,
9 type_ref::TypeRef, 9 type_ref::TypeRef,
10 nameres::{ModuleScope, lower::ImportId}, 10 nameres::{ModuleScope, lower::ImportId},
11 db::HirDatabase, 11 db::HirDatabase,
12 expr::BodySyntaxMapping, 12 expr::BodySyntaxMapping,
13 ty::InferenceResult, 13 ty::{InferenceResult, VariantDef},
14 adt::VariantData, 14 adt::VariantData,
15 generics::GenericParams, 15 generics::GenericParams,
16 code_model_impl::def_id_to_ast, 16 docs::{Documentation, Docs, docs_from_ast},
17 docs::{Documentation, Docs, docs_from_ast} 17 module_tree::ModuleId,
18 ids::{FunctionId, StructId, EnumId, EnumVariantId, AstItemDef, ConstId, StaticId, TraitId, TypeId},
18}; 19};
19 20
20/// hir::Crate describes a single crate. It's the main interface with which 21/// hir::Crate describes a single crate. It's the main interface with which
21/// a crate's dependencies interact. Mostly, it should be just a proxy for the 22/// a crate's dependencies interact. Mostly, it should be just a proxy for the
22/// root module. 23/// root module.
23#[derive(Debug, Clone, PartialEq, Eq, Hash)] 24#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
24pub struct Crate { 25pub struct Crate {
25 pub(crate) crate_id: CrateId, 26 pub(crate) crate_id: CrateId,
26} 27}
@@ -45,22 +46,40 @@ impl Crate {
45 46
46#[derive(Debug)] 47#[derive(Debug)]
47pub enum Def { 48pub enum Def {
49 Item,
50}
51
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
53pub struct Module {
54 pub(crate) krate: CrateId,
55 pub(crate) module_id: ModuleId,
56}
57
58/// The defs which can be visible in the module.
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
60pub enum ModuleDef {
48 Module(Module), 61 Module(Module),
62 Function(Function),
49 Struct(Struct), 63 Struct(Struct),
50 Enum(Enum), 64 Enum(Enum),
65 // Can't be directly declared, but can be imported.
51 EnumVariant(EnumVariant), 66 EnumVariant(EnumVariant),
52 Function(Function),
53 Const(Const), 67 Const(Const),
54 Static(Static), 68 Static(Static),
55 Trait(Trait), 69 Trait(Trait),
56 Type(Type), 70 Type(Type),
57 Item,
58}
59
60#[derive(Debug, Clone, PartialEq, Eq, Hash)]
61pub struct Module {
62 pub(crate) def_id: DefId,
63} 71}
72impl_froms!(
73 ModuleDef: Module,
74 Function,
75 Struct,
76 Enum,
77 EnumVariant,
78 Const,
79 Static,
80 Trait,
81 Type
82);
64 83
65pub enum ModuleSource { 84pub enum ModuleSource {
66 SourceFile(TreeArc<ast::SourceFile>), 85 SourceFile(TreeArc<ast::SourceFile>),
@@ -149,7 +168,7 @@ impl Module {
149 self.scope_impl(db) 168 self.scope_impl(db)
150 } 169 }
151 170
152 pub fn resolve_path(&self, db: &impl HirDatabase, path: &Path) -> PerNs<DefId> { 171 pub fn resolve_path(&self, db: &impl HirDatabase, path: &Path) -> PerNs<ModuleDef> {
153 self.resolve_path_impl(db, path) 172 self.resolve_path_impl(db, path)
154 } 173 }
155 174
@@ -160,7 +179,7 @@ impl Module {
160 179
161#[derive(Debug, Clone, PartialEq, Eq, Hash)] 180#[derive(Debug, Clone, PartialEq, Eq, Hash)]
162pub struct StructField { 181pub struct StructField {
163 parent: DefId, 182 parent: VariantDef,
164 name: Name, 183 name: Name,
165} 184}
166 185
@@ -174,38 +193,38 @@ impl StructField {
174 } 193 }
175} 194}
176 195
177#[derive(Debug, Clone, PartialEq, Eq, Hash)] 196#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
178pub struct Struct { 197pub struct Struct {
179 pub(crate) def_id: DefId, 198 pub(crate) id: StructId,
180} 199}
181 200
182impl Struct { 201impl Struct {
183 pub fn def_id(&self) -> DefId { 202 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::StructDef>) {
184 self.def_id 203 self.id.source(db)
204 }
205
206 pub fn module(&self, db: &impl HirDatabase) -> Module {
207 self.id.module(db)
185 } 208 }
186 209
187 pub fn name(&self, db: &impl HirDatabase) -> Option<Name> { 210 pub fn name(&self, db: &impl HirDatabase) -> Option<Name> {
188 db.struct_data(self.def_id).name.clone() 211 db.struct_data(*self).name.clone()
189 } 212 }
190 213
191 pub fn fields(&self, db: &impl HirDatabase) -> Vec<StructField> { 214 pub fn fields(&self, db: &impl HirDatabase) -> Vec<StructField> {
192 db.struct_data(self.def_id) 215 db.struct_data(*self)
193 .variant_data 216 .variant_data
194 .fields() 217 .fields()
195 .iter() 218 .iter()
196 .map(|it| StructField { 219 .map(|it| StructField {
197 parent: self.def_id, 220 parent: (*self).into(),
198 name: it.name.clone(), 221 name: it.name.clone(),
199 }) 222 })
200 .collect() 223 .collect()
201 } 224 }
202 225
203 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::StructDef>) {
204 def_id_to_ast(db, self.def_id)
205 }
206
207 pub fn generic_params(&self, db: &impl HirDatabase) -> Arc<GenericParams> { 226 pub fn generic_params(&self, db: &impl HirDatabase) -> Arc<GenericParams> {
208 db.generic_params(self.def_id) 227 db.generic_params((*self).into())
209 } 228 }
210} 229}
211 230
@@ -215,34 +234,30 @@ impl Docs for Struct {
215 } 234 }
216} 235}
217 236
218#[derive(Debug, Clone, PartialEq, Eq, Hash)] 237#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
219pub struct Enum { 238pub struct Enum {
220 pub(crate) def_id: DefId, 239 pub(crate) id: EnumId,
221} 240}
222 241
223impl Enum { 242impl Enum {
224 pub(crate) fn new(def_id: DefId) -> Self { 243 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumDef>) {
225 Enum { def_id } 244 self.id.source(db)
226 } 245 }
227 246
228 pub fn def_id(&self) -> DefId { 247 pub fn module(&self, db: &impl HirDatabase) -> Module {
229 self.def_id 248 self.id.module(db)
230 } 249 }
231 250
232 pub fn name(&self, db: &impl HirDatabase) -> Option<Name> { 251 pub fn name(&self, db: &impl HirDatabase) -> Option<Name> {
233 db.enum_data(self.def_id).name.clone() 252 db.enum_data(*self).name.clone()
234 } 253 }
235 254
236 pub fn variants(&self, db: &impl HirDatabase) -> Vec<(Name, EnumVariant)> { 255 pub fn variants(&self, db: &impl HirDatabase) -> Vec<(Name, EnumVariant)> {
237 db.enum_data(self.def_id).variants.clone() 256 db.enum_data(*self).variants.clone()
238 }
239
240 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumDef>) {
241 def_id_to_ast(db, self.def_id)
242 } 257 }
243 258
244 pub fn generic_params(&self, db: &impl HirDatabase) -> Arc<GenericParams> { 259 pub fn generic_params(&self, db: &impl HirDatabase) -> Arc<GenericParams> {
245 db.generic_params(self.def_id) 260 db.generic_params((*self).into())
246 } 261 }
247} 262}
248 263
@@ -252,30 +267,28 @@ impl Docs for Enum {
252 } 267 }
253} 268}
254 269
255#[derive(Debug, Clone, PartialEq, Eq, Hash)] 270#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
256pub struct EnumVariant { 271pub struct EnumVariant {
257 pub(crate) def_id: DefId, 272 pub(crate) id: EnumVariantId,
258} 273}
259 274
260impl EnumVariant { 275impl EnumVariant {
261 pub(crate) fn new(def_id: DefId) -> Self { 276 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumVariant>) {
262 EnumVariant { def_id } 277 self.id.source(db)
263 } 278 }
264 279 pub fn module(&self, db: &impl HirDatabase) -> Module {
265 pub fn def_id(&self) -> DefId { 280 self.id.module(db)
266 self.def_id
267 } 281 }
268
269 pub fn parent_enum(&self, db: &impl HirDatabase) -> Enum { 282 pub fn parent_enum(&self, db: &impl HirDatabase) -> Enum {
270 db.enum_variant_data(self.def_id).parent_enum.clone() 283 db.enum_variant_data(*self).parent_enum.clone()
271 } 284 }
272 285
273 pub fn name(&self, db: &impl HirDatabase) -> Option<Name> { 286 pub fn name(&self, db: &impl HirDatabase) -> Option<Name> {
274 db.enum_variant_data(self.def_id).name.clone() 287 db.enum_variant_data(*self).name.clone()
275 } 288 }
276 289
277 pub fn variant_data(&self, db: &impl HirDatabase) -> Arc<VariantData> { 290 pub fn variant_data(&self, db: &impl HirDatabase) -> Arc<VariantData> {
278 db.enum_variant_data(self.def_id).variant_data.clone() 291 db.enum_variant_data(*self).variant_data.clone()
279 } 292 }
280 293
281 pub fn fields(&self, db: &impl HirDatabase) -> Vec<StructField> { 294 pub fn fields(&self, db: &impl HirDatabase) -> Vec<StructField> {
@@ -283,15 +296,11 @@ impl EnumVariant {
283 .fields() 296 .fields()
284 .iter() 297 .iter()
285 .map(|it| StructField { 298 .map(|it| StructField {
286 parent: self.def_id, 299 parent: (*self).into(),
287 name: it.name.clone(), 300 name: it.name.clone(),
288 }) 301 })
289 .collect() 302 .collect()
290 } 303 }
291
292 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumVariant>) {
293 def_id_to_ast(db, self.def_id)
294 }
295} 304}
296 305
297impl Docs for EnumVariant { 306impl Docs for EnumVariant {
@@ -300,9 +309,9 @@ impl Docs for EnumVariant {
300 } 309 }
301} 310}
302 311
303#[derive(Debug, Clone, PartialEq, Eq, Hash)] 312#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
304pub struct Function { 313pub struct Function {
305 pub(crate) def_id: DefId, 314 pub(crate) id: FunctionId,
306} 315}
307 316
308pub use crate::code_model_impl::function::ScopeEntryWithSyntax; 317pub use crate::code_model_impl::function::ScopeEntryWithSyntax;
@@ -339,21 +348,21 @@ impl FnSignature {
339} 348}
340 349
341impl Function { 350impl Function {
342 pub fn def_id(&self) -> DefId { 351 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::FnDef>) {
343 self.def_id 352 self.id.source(db)
344 } 353 }
345 354
346 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::FnDef>) { 355 pub fn module(&self, db: &impl HirDatabase) -> Module {
347 def_id_to_ast(db, self.def_id) 356 self.id.module(db)
348 } 357 }
349 358
350 pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Arc<BodySyntaxMapping> { 359 pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Arc<BodySyntaxMapping> {
351 db.body_syntax_mapping(self.def_id) 360 db.body_syntax_mapping(*self)
352 } 361 }
353 362
354 pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSyntaxMapping { 363 pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSyntaxMapping {
355 let scopes = db.fn_scopes(self.def_id); 364 let scopes = db.fn_scopes(*self);
356 let syntax_mapping = db.body_syntax_mapping(self.def_id); 365 let syntax_mapping = db.body_syntax_mapping(*self);
357 ScopesWithSyntaxMapping { 366 ScopesWithSyntaxMapping {
358 scopes, 367 scopes,
359 syntax_mapping, 368 syntax_mapping,
@@ -361,15 +370,15 @@ impl Function {
361 } 370 }
362 371
363 pub fn signature(&self, db: &impl HirDatabase) -> Arc<FnSignature> { 372 pub fn signature(&self, db: &impl HirDatabase) -> Arc<FnSignature> {
364 db.fn_signature(self.def_id) 373 db.fn_signature(*self)
365 } 374 }
366 375
367 pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> { 376 pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> {
368 db.infer(self.def_id) 377 db.infer(*self)
369 } 378 }
370 379
371 pub fn generic_params(&self, db: &impl HirDatabase) -> Arc<GenericParams> { 380 pub fn generic_params(&self, db: &impl HirDatabase) -> Arc<GenericParams> {
372 db.generic_params(self.def_id) 381 db.generic_params((*self).into())
373 } 382 }
374} 383}
375 384
@@ -379,18 +388,14 @@ impl Docs for Function {
379 } 388 }
380} 389}
381 390
382#[derive(Debug, Clone, PartialEq, Eq, Hash)] 391#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
383pub struct Const { 392pub struct Const {
384 pub(crate) def_id: DefId, 393 pub(crate) id: ConstId,
385} 394}
386 395
387impl Const { 396impl Const {
388 pub(crate) fn new(def_id: DefId) -> Const {
389 Const { def_id }
390 }
391
392 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::ConstDef>) { 397 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::ConstDef>) {
393 def_id_to_ast(db, self.def_id) 398 self.id.source(db)
394 } 399 }
395} 400}
396 401
@@ -400,18 +405,14 @@ impl Docs for Const {
400 } 405 }
401} 406}
402 407
403#[derive(Debug, Clone, PartialEq, Eq, Hash)] 408#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
404pub struct Static { 409pub struct Static {
405 pub(crate) def_id: DefId, 410 pub(crate) id: StaticId,
406} 411}
407 412
408impl Static { 413impl Static {
409 pub(crate) fn new(def_id: DefId) -> Static {
410 Static { def_id }
411 }
412
413 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::StaticDef>) { 414 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::StaticDef>) {
414 def_id_to_ast(db, self.def_id) 415 self.id.source(db)
415 } 416 }
416} 417}
417 418
@@ -421,22 +422,18 @@ impl Docs for Static {
421 } 422 }
422} 423}
423 424
424#[derive(Debug, Clone, PartialEq, Eq, Hash)] 425#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
425pub struct Trait { 426pub struct Trait {
426 pub(crate) def_id: DefId, 427 pub(crate) id: TraitId,
427} 428}
428 429
429impl Trait { 430impl Trait {
430 pub(crate) fn new(def_id: DefId) -> Trait {
431 Trait { def_id }
432 }
433
434 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::TraitDef>) { 431 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::TraitDef>) {
435 def_id_to_ast(db, self.def_id) 432 self.id.source(db)
436 } 433 }
437 434
438 pub fn generic_params(&self, db: &impl HirDatabase) -> Arc<GenericParams> { 435 pub fn generic_params(&self, db: &impl HirDatabase) -> Arc<GenericParams> {
439 db.generic_params(self.def_id) 436 db.generic_params((*self).into())
440 } 437 }
441} 438}
442 439
@@ -446,22 +443,18 @@ impl Docs for Trait {
446 } 443 }
447} 444}
448 445
449#[derive(Debug, Clone, PartialEq, Eq, Hash)] 446#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
450pub struct Type { 447pub struct Type {
451 pub(crate) def_id: DefId, 448 pub(crate) id: TypeId,
452} 449}
453 450
454impl Type { 451impl Type {
455 pub(crate) fn new(def_id: DefId) -> Type {
456 Type { def_id }
457 }
458
459 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::TypeDef>) { 452 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::TypeDef>) {
460 def_id_to_ast(db, self.def_id) 453 self.id.source(db)
461 } 454 }
462 455
463 pub fn generic_params(&self, db: &impl HirDatabase) -> Arc<GenericParams> { 456 pub fn generic_params(&self, db: &impl HirDatabase) -> Arc<GenericParams> {
464 db.generic_params(self.def_id) 457 db.generic_params((*self).into())
465 } 458 }
466} 459}
467 460