aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model.rs
diff options
context:
space:
mode:
authorSeivan Heidari <seivan.heidari@icloud.com>2019-10-31 08:43:20 +0000
committerSeivan Heidari <seivan.heidari@icloud.com>2019-10-31 08:43:20 +0000
commit8edda0e7b164009d6c03bb3d4be603fb38ad2e2a (patch)
tree744cf81075d394e2f9c06afb07642a2601800dda /crates/ra_hir/src/code_model.rs
parent49562d36b97ddde34cf7585a8c2e8f232519b657 (diff)
parentd067afb064a7fa67b172abf561b7d80740cd6f18 (diff)
Merge branch 'master' into feature/themes
Diffstat (limited to 'crates/ra_hir/src/code_model.rs')
-rw-r--r--crates/ra_hir/src/code_model.rs182
1 files changed, 61 insertions, 121 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 8eb3c577d..b32aa145e 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -5,11 +5,17 @@ pub(crate) mod docs;
5 5
6use std::sync::Arc; 6use std::sync::Arc;
7 7
8use ra_db::{CrateId, Edition, FileId}; 8use hir_def::{
9 builtin_type::BuiltinType,
10 type_ref::{Mutability, TypeRef},
11 CrateModuleId, LocalEnumVariantId, ModuleId,
12};
13use hir_expand::name::{self, AsName};
14use ra_db::{CrateId, Edition};
9use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; 15use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner};
10 16
11use crate::{ 17use crate::{
12 adt::{EnumVariantId, StructFieldId, VariantDef}, 18 adt::{StructFieldId, VariantDef},
13 db::{AstDatabase, DefDatabase, HirDatabase}, 19 db::{AstDatabase, DefDatabase, HirDatabase},
14 diagnostics::DiagnosticSink, 20 diagnostics::DiagnosticSink,
15 expr::{validation::ExprValidator, Body, BodySourceMap}, 21 expr::{validation::ExprValidator, Body, BodySourceMap},
@@ -19,20 +25,11 @@ use crate::{
19 TypeAliasId, 25 TypeAliasId,
20 }, 26 },
21 impl_block::ImplBlock, 27 impl_block::ImplBlock,
22 name::{ 28 nameres::{ImportId, ModuleScope, Namespace},
23 BOOL, CHAR, F32, F64, I128, I16, I32, I64, I8, ISIZE, SELF_TYPE, STR, U128, U16, U32, U64,
24 U8, USIZE,
25 },
26 nameres::{CrateModuleId, ImportId, ModuleScope, Namespace},
27 resolve::{Resolver, Scope, TypeNs}, 29 resolve::{Resolver, Scope, TypeNs},
28 traits::TraitData, 30 traits::TraitData,
29 ty::{ 31 ty::{InferenceResult, TraitRef},
30 primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness}, 32 Either, HasSource, Name, Ty,
31 InferenceResult, TraitRef,
32 },
33 type_ref::Mutability,
34 type_ref::TypeRef,
35 AsName, AstId, Either, HasSource, Name, Ty,
36}; 33};
37 34
38/// hir::Crate describes a single crate. It's the main interface with which 35/// hir::Crate describes a single crate. It's the main interface with which
@@ -67,8 +64,7 @@ impl Crate {
67 64
68 pub fn root_module(self, db: &impl DefDatabase) -> Option<Module> { 65 pub fn root_module(self, db: &impl DefDatabase) -> Option<Module> {
69 let module_id = db.crate_def_map(self).root(); 66 let module_id = db.crate_def_map(self).root();
70 let module = Module { krate: self, module_id }; 67 Some(Module::new(self, module_id))
71 Some(module)
72 } 68 }
73 69
74 pub fn edition(self, db: &impl DefDatabase) -> Edition { 70 pub fn edition(self, db: &impl DefDatabase) -> Edition {
@@ -83,43 +79,7 @@ impl Crate {
83 79
84#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 80#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
85pub struct Module { 81pub struct Module {
86 pub(crate) krate: Crate, 82 pub(crate) id: ModuleId,
87 pub(crate) module_id: CrateModuleId,
88}
89
90#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
91pub enum BuiltinType {
92 Char,
93 Bool,
94 Str,
95 Int(IntTy),
96 Float(FloatTy),
97}
98
99impl BuiltinType {
100 #[rustfmt::skip]
101 pub(crate) const ALL: &'static [(Name, BuiltinType)] = &[
102 (CHAR, BuiltinType::Char),
103 (BOOL, BuiltinType::Bool),
104 (STR, BuiltinType::Str),
105
106 (ISIZE, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::Xsize })),
107 (I8, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X8 })),
108 (I16, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X16 })),
109 (I32, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X32 })),
110 (I64, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X64 })),
111 (I128, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X128 })),
112
113 (USIZE, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::Xsize })),
114 (U8, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X8 })),
115 (U16, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X16 })),
116 (U32, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X32 })),
117 (U64, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X64 })),
118 (U128, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X128 })),
119
120 (F32, BuiltinType::Float(FloatTy { bitness: FloatBitness::X32 })),
121 (F64, BuiltinType::Float(FloatTy { bitness: FloatBitness::X64 })),
122 ];
123} 83}
124 84
125/// The defs which can be visible in the module. 85/// The defs which can be visible in the module.
@@ -148,39 +108,19 @@ impl_froms!(
148 BuiltinType 108 BuiltinType
149); 109);
150 110
151pub enum ModuleSource { 111pub use hir_def::ModuleSource;
152 SourceFile(ast::SourceFile),
153 Module(ast::Module),
154}
155 112
156impl ModuleSource { 113impl Module {
157 pub(crate) fn new( 114 pub(crate) fn new(krate: Crate, crate_module_id: CrateModuleId) -> Module {
158 db: &(impl DefDatabase + AstDatabase), 115 Module { id: ModuleId { krate: krate.crate_id, module_id: crate_module_id } }
159 file_id: Option<FileId>,
160 decl_id: Option<AstId<ast::Module>>,
161 ) -> ModuleSource {
162 match (file_id, decl_id) {
163 (Some(file_id), _) => {
164 let source_file = db.parse(file_id).tree();
165 ModuleSource::SourceFile(source_file)
166 }
167 (None, Some(item_id)) => {
168 let module = item_id.to_node(db);
169 assert!(module.item_list().is_some(), "expected inline module");
170 ModuleSource::Module(module)
171 }
172 (None, None) => panic!(),
173 }
174 } 116 }
175}
176 117
177impl Module {
178 /// Name of this module. 118 /// Name of this module.
179 pub fn name(self, db: &impl DefDatabase) -> Option<Name> { 119 pub fn name(self, db: &impl DefDatabase) -> Option<Name> {
180 let def_map = db.crate_def_map(self.krate); 120 let def_map = db.crate_def_map(self.krate());
181 let parent = def_map[self.module_id].parent?; 121 let parent = def_map[self.id.module_id].parent?;
182 def_map[parent].children.iter().find_map(|(name, module_id)| { 122 def_map[parent].children.iter().find_map(|(name, module_id)| {
183 if *module_id == self.module_id { 123 if *module_id == self.id.module_id {
184 Some(name.clone()) 124 Some(name.clone())
185 } else { 125 } else {
186 None 126 None
@@ -200,29 +140,29 @@ impl Module {
200 } 140 }
201 141
202 /// Returns the crate this module is part of. 142 /// Returns the crate this module is part of.
203 pub fn krate(self, _db: &impl DefDatabase) -> Option<Crate> { 143 pub fn krate(self) -> Crate {
204 Some(self.krate) 144 Crate { crate_id: self.id.krate }
205 } 145 }
206 146
207 /// Topmost parent of this module. Every module has a `crate_root`, but some 147 /// Topmost parent of this module. Every module has a `crate_root`, but some
208 /// might be missing `krate`. This can happen if a module's file is not included 148 /// might be missing `krate`. This can happen if a module's file is not included
209 /// in the module tree of any target in `Cargo.toml`. 149 /// in the module tree of any target in `Cargo.toml`.
210 pub fn crate_root(self, db: &impl DefDatabase) -> Module { 150 pub fn crate_root(self, db: &impl DefDatabase) -> Module {
211 let def_map = db.crate_def_map(self.krate); 151 let def_map = db.crate_def_map(self.krate());
212 self.with_module_id(def_map.root()) 152 self.with_module_id(def_map.root())
213 } 153 }
214 154
215 /// Finds a child module with the specified name. 155 /// Finds a child module with the specified name.
216 pub fn child(self, db: &impl HirDatabase, name: &Name) -> Option<Module> { 156 pub fn child(self, db: &impl HirDatabase, name: &Name) -> Option<Module> {
217 let def_map = db.crate_def_map(self.krate); 157 let def_map = db.crate_def_map(self.krate());
218 let child_id = def_map[self.module_id].children.get(name)?; 158 let child_id = def_map[self.id.module_id].children.get(name)?;
219 Some(self.with_module_id(*child_id)) 159 Some(self.with_module_id(*child_id))
220 } 160 }
221 161
222 /// Iterates over all child modules. 162 /// Iterates over all child modules.
223 pub fn children(self, db: &impl DefDatabase) -> impl Iterator<Item = Module> { 163 pub fn children(self, db: &impl DefDatabase) -> impl Iterator<Item = Module> {
224 let def_map = db.crate_def_map(self.krate); 164 let def_map = db.crate_def_map(self.krate());
225 let children = def_map[self.module_id] 165 let children = def_map[self.id.module_id]
226 .children 166 .children
227 .iter() 167 .iter()
228 .map(|(_, module_id)| self.with_module_id(*module_id)) 168 .map(|(_, module_id)| self.with_module_id(*module_id))
@@ -232,8 +172,8 @@ impl Module {
232 172
233 /// Finds a parent module. 173 /// Finds a parent module.
234 pub fn parent(self, db: &impl DefDatabase) -> Option<Module> { 174 pub fn parent(self, db: &impl DefDatabase) -> Option<Module> {
235 let def_map = db.crate_def_map(self.krate); 175 let def_map = db.crate_def_map(self.krate());
236 let parent_id = def_map[self.module_id].parent?; 176 let parent_id = def_map[self.id.module_id].parent?;
237 Some(self.with_module_id(parent_id)) 177 Some(self.with_module_id(parent_id))
238 } 178 }
239 179
@@ -249,11 +189,11 @@ impl Module {
249 189
250 /// Returns a `ModuleScope`: a set of items, visible in this module. 190 /// Returns a `ModuleScope`: a set of items, visible in this module.
251 pub fn scope(self, db: &impl HirDatabase) -> ModuleScope { 191 pub fn scope(self, db: &impl HirDatabase) -> ModuleScope {
252 db.crate_def_map(self.krate)[self.module_id].scope.clone() 192 db.crate_def_map(self.krate())[self.id.module_id].scope.clone()
253 } 193 }
254 194
255 pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) { 195 pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) {
256 db.crate_def_map(self.krate).add_diagnostics(db, self.module_id, sink); 196 db.crate_def_map(self.krate()).add_diagnostics(db, self.id.module_id, sink);
257 for decl in self.declarations(db) { 197 for decl in self.declarations(db) {
258 match decl { 198 match decl {
259 crate::ModuleDef::Function(f) => f.diagnostics(db, sink), 199 crate::ModuleDef::Function(f) => f.diagnostics(db, sink),
@@ -277,13 +217,13 @@ impl Module {
277 } 217 }
278 218
279 pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver { 219 pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver {
280 let def_map = db.crate_def_map(self.krate); 220 let def_map = db.crate_def_map(self.krate());
281 Resolver::default().push_module_scope(def_map, self.module_id) 221 Resolver::default().push_module_scope(def_map, self.id.module_id)
282 } 222 }
283 223
284 pub fn declarations(self, db: &impl DefDatabase) -> Vec<ModuleDef> { 224 pub fn declarations(self, db: &impl DefDatabase) -> Vec<ModuleDef> {
285 let def_map = db.crate_def_map(self.krate); 225 let def_map = db.crate_def_map(self.krate());
286 def_map[self.module_id] 226 def_map[self.id.module_id]
287 .scope 227 .scope
288 .entries() 228 .entries()
289 .filter_map(|(_name, res)| if res.import.is_none() { Some(res.def) } else { None }) 229 .filter_map(|(_name, res)| if res.import.is_none() { Some(res.def) } else { None })
@@ -303,7 +243,7 @@ impl Module {
303 } 243 }
304 244
305 fn with_module_id(self, module_id: CrateModuleId) -> Module { 245 fn with_module_id(self, module_id: CrateModuleId) -> Module {
306 Module { module_id, krate: self.krate } 246 Module::new(self.krate(), module_id)
307 } 247 }
308} 248}
309 249
@@ -340,11 +280,11 @@ pub struct Struct {
340 280
341impl Struct { 281impl Struct {
342 pub fn module(self, db: &impl DefDatabase) -> Module { 282 pub fn module(self, db: &impl DefDatabase) -> Module {
343 self.id.module(db) 283 Module { id: self.id.module(db) }
344 } 284 }
345 285
346 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 286 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
347 self.module(db).krate(db) 287 Some(self.module(db).krate())
348 } 288 }
349 289
350 pub fn name(self, db: &impl DefDatabase) -> Option<Name> { 290 pub fn name(self, db: &impl DefDatabase) -> Option<Name> {
@@ -402,7 +342,7 @@ impl Union {
402 } 342 }
403 343
404 pub fn module(self, db: &impl HirDatabase) -> Module { 344 pub fn module(self, db: &impl HirDatabase) -> Module {
405 self.id.module(db) 345 Module { id: self.id.module(db) }
406 } 346 }
407 347
408 pub fn ty(self, db: &impl HirDatabase) -> Ty { 348 pub fn ty(self, db: &impl HirDatabase) -> Ty {
@@ -428,11 +368,11 @@ pub struct Enum {
428 368
429impl Enum { 369impl Enum {
430 pub fn module(self, db: &impl DefDatabase) -> Module { 370 pub fn module(self, db: &impl DefDatabase) -> Module {
431 self.id.module(db) 371 Module { id: self.id.module(db) }
432 } 372 }
433 373
434 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 374 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
435 self.module(db).krate(db) 375 Some(self.module(db).krate())
436 } 376 }
437 377
438 pub fn name(self, db: &impl DefDatabase) -> Option<Name> { 378 pub fn name(self, db: &impl DefDatabase) -> Option<Name> {
@@ -470,7 +410,7 @@ impl Enum {
470#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 410#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
471pub struct EnumVariant { 411pub struct EnumVariant {
472 pub(crate) parent: Enum, 412 pub(crate) parent: Enum,
473 pub(crate) id: EnumVariantId, 413 pub(crate) id: LocalEnumVariantId,
474} 414}
475 415
476impl EnumVariant { 416impl EnumVariant {
@@ -523,12 +463,14 @@ impl Adt {
523 } 463 }
524 464
525 pub fn krate(self, db: &impl HirDatabase) -> Option<Crate> { 465 pub fn krate(self, db: &impl HirDatabase) -> Option<Crate> {
526 match self { 466 Some(
527 Adt::Struct(s) => s.module(db), 467 match self {
528 Adt::Union(s) => s.module(db), 468 Adt::Struct(s) => s.module(db),
529 Adt::Enum(e) => e.module(db), 469 Adt::Union(s) => s.module(db),
530 } 470 Adt::Enum(e) => e.module(db),
531 .krate(db) 471 }
472 .krate(),
473 )
532 } 474 }
533 475
534 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver { 476 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {
@@ -643,7 +585,7 @@ impl FnData {
643 let self_type = if let Some(type_ref) = self_param.ascribed_type() { 585 let self_type = if let Some(type_ref) = self_param.ascribed_type() {
644 TypeRef::from_ast(type_ref) 586 TypeRef::from_ast(type_ref)
645 } else { 587 } else {
646 let self_type = TypeRef::Path(SELF_TYPE.into()); 588 let self_type = TypeRef::Path(name::SELF_TYPE.into());
647 match self_param.kind() { 589 match self_param.kind() {
648 ast::SelfParamKind::Owned => self_type, 590 ast::SelfParamKind::Owned => self_type,
649 ast::SelfParamKind::Ref => { 591 ast::SelfParamKind::Ref => {
@@ -692,11 +634,11 @@ impl FnData {
692 634
693impl Function { 635impl Function {
694 pub fn module(self, db: &impl DefDatabase) -> Module { 636 pub fn module(self, db: &impl DefDatabase) -> Module {
695 self.id.module(db) 637 Module { id: self.id.module(db) }
696 } 638 }
697 639
698 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 640 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
699 self.module(db).krate(db) 641 Some(self.module(db).krate())
700 } 642 }
701 643
702 pub fn name(self, db: &impl HirDatabase) -> Name { 644 pub fn name(self, db: &impl HirDatabase) -> Name {
@@ -770,11 +712,11 @@ pub struct Const {
770 712
771impl Const { 713impl Const {
772 pub fn module(self, db: &impl DefDatabase) -> Module { 714 pub fn module(self, db: &impl DefDatabase) -> Module {
773 self.id.module(db) 715 Module { id: self.id.module(db) }
774 } 716 }
775 717
776 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 718 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
777 self.module(db).krate(db) 719 Some(self.module(db).krate())
778 } 720 }
779 721
780 pub fn data(self, db: &impl HirDatabase) -> Arc<ConstData> { 722 pub fn data(self, db: &impl HirDatabase) -> Arc<ConstData> {
@@ -867,11 +809,11 @@ pub struct Static {
867 809
868impl Static { 810impl Static {
869 pub fn module(self, db: &impl DefDatabase) -> Module { 811 pub fn module(self, db: &impl DefDatabase) -> Module {
870 self.id.module(db) 812 Module { id: self.id.module(db) }
871 } 813 }
872 814
873 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 815 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
874 self.module(db).krate(db) 816 Some(self.module(db).krate())
875 } 817 }
876 818
877 pub fn data(self, db: &impl HirDatabase) -> Arc<ConstData> { 819 pub fn data(self, db: &impl HirDatabase) -> Arc<ConstData> {
@@ -896,7 +838,7 @@ pub struct Trait {
896 838
897impl Trait { 839impl Trait {
898 pub fn module(self, db: &impl DefDatabase) -> Module { 840 pub fn module(self, db: &impl DefDatabase) -> Module {
899 self.id.module(db) 841 Module { id: self.id.module(db) }
900 } 842 }
901 843
902 pub fn name(self, db: &impl DefDatabase) -> Option<Name> { 844 pub fn name(self, db: &impl DefDatabase) -> Option<Name> {
@@ -917,9 +859,7 @@ impl Trait {
917 .where_predicates 859 .where_predicates
918 .iter() 860 .iter()
919 .filter_map(|pred| match &pred.type_ref { 861 .filter_map(|pred| match &pred.type_ref {
920 TypeRef::Path(p) if p.as_ident() == Some(&crate::name::SELF_TYPE) => { 862 TypeRef::Path(p) if p.as_ident() == Some(&name::SELF_TYPE) => pred.bound.as_path(),
921 pred.bound.as_path()
922 }
923 _ => None, 863 _ => None,
924 }) 864 })
925 .filter_map(|path| match resolver.resolve_path_in_type_ns_fully(db, path) { 865 .filter_map(|path| match resolver.resolve_path_in_type_ns_fully(db, path) {
@@ -998,11 +938,11 @@ pub struct TypeAlias {
998 938
999impl TypeAlias { 939impl TypeAlias {
1000 pub fn module(self, db: &impl DefDatabase) -> Module { 940 pub fn module(self, db: &impl DefDatabase) -> Module {
1001 self.id.module(db) 941 Module { id: self.id.module(db) }
1002 } 942 }
1003 943
1004 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 944 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
1005 self.module(db).krate(db) 945 Some(self.module(db).krate())
1006 } 946 }
1007 947
1008 /// The containing impl block, if this is a method. 948 /// The containing impl block, if this is a method.