diff options
Diffstat (limited to 'crates/ra_hir')
-rw-r--r-- | crates/ra_hir/src/code_model_api.rs | 38 | ||||
-rw-r--r-- | crates/ra_hir/src/code_model_impl/module.rs | 6 | ||||
-rw-r--r-- | crates/ra_hir/src/ids.rs | 32 | ||||
-rw-r--r-- | crates/ra_hir/src/nameres/lower.rs | 24 | ||||
-rw-r--r-- | crates/ra_hir/src/ty.rs | 5 |
5 files changed, 68 insertions, 37 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 1fa591ea4..d82dda79a 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs | |||
@@ -16,7 +16,7 @@ use crate::{ | |||
16 | code_model_impl::def_id_to_ast, | 16 | code_model_impl::def_id_to_ast, |
17 | docs::{Documentation, Docs, docs_from_ast}, | 17 | docs::{Documentation, Docs, docs_from_ast}, |
18 | module_tree::ModuleId, | 18 | module_tree::ModuleId, |
19 | ids::{FunctionId, StructId, EnumId, EnumVariantId, AstItemDef}, | 19 | ids::{FunctionId, StructId, EnumId, EnumVariantId, AstItemDef, ConstId, StaticId}, |
20 | }; | 20 | }; |
21 | 21 | ||
22 | /// hir::Crate describes a single crate. It's the main interface with which | 22 | /// hir::Crate describes a single crate. It's the main interface with which |
@@ -47,8 +47,6 @@ impl Crate { | |||
47 | 47 | ||
48 | #[derive(Debug)] | 48 | #[derive(Debug)] |
49 | pub enum Def { | 49 | pub enum Def { |
50 | Const(Const), | ||
51 | Static(Static), | ||
52 | Trait(Trait), | 50 | Trait(Trait), |
53 | Type(Type), | 51 | Type(Type), |
54 | Item, | 52 | Item, |
@@ -67,11 +65,21 @@ pub enum ModuleDef { | |||
67 | Function(Function), | 65 | Function(Function), |
68 | Struct(Struct), | 66 | Struct(Struct), |
69 | Enum(Enum), | 67 | Enum(Enum), |
70 | // Can't be directly declared, but can be imported. | ||
71 | EnumVariant(EnumVariant), | 68 | EnumVariant(EnumVariant), |
69 | Const(Const), | ||
70 | Static(Static), | ||
71 | // Can't be directly declared, but can be imported. | ||
72 | Def(DefId), | 72 | Def(DefId), |
73 | } | 73 | } |
74 | impl_froms!(ModuleDef: Module, Function, Struct, Enum, EnumVariant); | 74 | impl_froms!( |
75 | ModuleDef: Module, | ||
76 | Function, | ||
77 | Struct, | ||
78 | Enum, | ||
79 | EnumVariant, | ||
80 | Const, | ||
81 | Static | ||
82 | ); | ||
75 | 83 | ||
76 | impl From<DefId> for ModuleDef { | 84 | impl From<DefId> for ModuleDef { |
77 | fn from(it: DefId) -> ModuleDef { | 85 | fn from(it: DefId) -> ModuleDef { |
@@ -386,18 +394,14 @@ impl Docs for Function { | |||
386 | } | 394 | } |
387 | } | 395 | } |
388 | 396 | ||
389 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 397 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
390 | pub struct Const { | 398 | pub struct Const { |
391 | pub(crate) def_id: DefId, | 399 | pub(crate) id: ConstId, |
392 | } | 400 | } |
393 | 401 | ||
394 | impl Const { | 402 | impl Const { |
395 | pub(crate) fn new(def_id: DefId) -> Const { | ||
396 | Const { def_id } | ||
397 | } | ||
398 | |||
399 | pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::ConstDef>) { | 403 | pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::ConstDef>) { |
400 | def_id_to_ast(db, self.def_id) | 404 | self.id.source(db) |
401 | } | 405 | } |
402 | } | 406 | } |
403 | 407 | ||
@@ -407,18 +411,14 @@ impl Docs for Const { | |||
407 | } | 411 | } |
408 | } | 412 | } |
409 | 413 | ||
410 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 414 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
411 | pub struct Static { | 415 | pub struct Static { |
412 | pub(crate) def_id: DefId, | 416 | pub(crate) id: StaticId, |
413 | } | 417 | } |
414 | 418 | ||
415 | impl Static { | 419 | impl Static { |
416 | pub(crate) fn new(def_id: DefId) -> Static { | ||
417 | Static { def_id } | ||
418 | } | ||
419 | |||
420 | pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::StaticDef>) { | 420 | pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::StaticDef>) { |
421 | def_id_to_ast(db, self.def_id) | 421 | self.id.source(db) |
422 | } | 422 | } |
423 | } | 423 | } |
424 | 424 | ||
diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs index aa5e5d689..1518825c7 100644 --- a/crates/ra_hir/src/code_model_impl/module.rs +++ b/crates/ra_hir/src/code_model_impl/module.rs | |||
@@ -147,7 +147,11 @@ impl Module { | |||
147 | None => PerNs::none(), | 147 | None => PerNs::none(), |
148 | } | 148 | } |
149 | } | 149 | } |
150 | ModuleDef::Function(_) | ModuleDef::Struct(_) | ModuleDef::EnumVariant(_) => { | 150 | ModuleDef::Function(_) |
151 | | ModuleDef::Struct(_) | ||
152 | | ModuleDef::Const(_) | ||
153 | | ModuleDef::Static(_) | ||
154 | | ModuleDef::EnumVariant(_) => { | ||
151 | // could be an inherent method call in UFCS form | 155 | // could be an inherent method call in UFCS form |
152 | // (`Struct::method`), or some other kind of associated | 156 | // (`Struct::method`), or some other kind of associated |
153 | // item... Which we currently don't handle (TODO) | 157 | // item... Which we currently don't handle (TODO) |
diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index 4b4e07e58..9aae58bb6 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs | |||
@@ -9,7 +9,7 @@ use ra_arena::{Arena, RawId, ArenaId, impl_arena_id}; | |||
9 | 9 | ||
10 | use crate::{ | 10 | use crate::{ |
11 | HirDatabase, Def, | 11 | HirDatabase, Def, |
12 | Module, Trait, Type, Static, Const, | 12 | Module, Trait, Type, |
13 | }; | 13 | }; |
14 | 14 | ||
15 | #[derive(Debug, Default)] | 15 | #[derive(Debug, Default)] |
@@ -20,6 +20,8 @@ pub struct HirInterner { | |||
20 | structs: LocationIntener<ItemLoc<ast::StructDef>, StructId>, | 20 | structs: LocationIntener<ItemLoc<ast::StructDef>, StructId>, |
21 | enums: LocationIntener<ItemLoc<ast::EnumDef>, EnumId>, | 21 | enums: LocationIntener<ItemLoc<ast::EnumDef>, EnumId>, |
22 | enum_variants: LocationIntener<ItemLoc<ast::EnumVariant>, EnumVariantId>, | 22 | enum_variants: LocationIntener<ItemLoc<ast::EnumVariant>, EnumVariantId>, |
23 | consts: LocationIntener<ItemLoc<ast::ConstDef>, ConstId>, | ||
24 | statics: LocationIntener<ItemLoc<ast::StaticDef>, StaticId>, | ||
23 | } | 25 | } |
24 | 26 | ||
25 | impl HirInterner { | 27 | impl HirInterner { |
@@ -246,6 +248,24 @@ impl AstItemDef<ast::EnumVariant> for EnumVariantId { | |||
246 | } | 248 | } |
247 | } | 249 | } |
248 | 250 | ||
251 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
252 | pub struct ConstId(RawId); | ||
253 | impl_arena_id!(ConstId); | ||
254 | impl AstItemDef<ast::ConstDef> for ConstId { | ||
255 | fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<ast::ConstDef>, Self> { | ||
256 | &interner.consts | ||
257 | } | ||
258 | } | ||
259 | |||
260 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
261 | pub struct StaticId(RawId); | ||
262 | impl_arena_id!(StaticId); | ||
263 | impl AstItemDef<ast::StaticDef> for StaticId { | ||
264 | fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<ast::StaticDef>, Self> { | ||
265 | &interner.statics | ||
266 | } | ||
267 | } | ||
268 | |||
249 | /// Def's are a core concept of hir. A `Def` is an Item (function, module, etc) | 269 | /// Def's are a core concept of hir. A `Def` is an Item (function, module, etc) |
250 | /// in a specific module. | 270 | /// in a specific module. |
251 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 271 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
@@ -261,8 +281,6 @@ pub struct DefLoc { | |||
261 | 281 | ||
262 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | 282 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
263 | pub(crate) enum DefKind { | 283 | pub(crate) enum DefKind { |
264 | Const, | ||
265 | Static, | ||
266 | Trait, | 284 | Trait, |
267 | Type, | 285 | Type, |
268 | Item, | 286 | Item, |
@@ -286,14 +304,6 @@ impl DefId { | |||
286 | pub fn resolve(self, db: &impl HirDatabase) -> Def { | 304 | pub fn resolve(self, db: &impl HirDatabase) -> Def { |
287 | let loc = self.loc(db); | 305 | let loc = self.loc(db); |
288 | match loc.kind { | 306 | match loc.kind { |
289 | DefKind::Const => { | ||
290 | let def = Const::new(self); | ||
291 | Def::Const(def) | ||
292 | } | ||
293 | DefKind::Static => { | ||
294 | let def = Static::new(self); | ||
295 | Def::Static(def) | ||
296 | } | ||
297 | DefKind::Trait => { | 307 | DefKind::Trait => { |
298 | let def = Trait::new(self); | 308 | let def = Trait::new(self); |
299 | Def::Trait(def) | 309 | Def::Trait(def) |
diff --git a/crates/ra_hir/src/nameres/lower.rs b/crates/ra_hir/src/nameres/lower.rs index 0056bdd5a..6a86e5fd4 100644 --- a/crates/ra_hir/src/nameres/lower.rs +++ b/crates/ra_hir/src/nameres/lower.rs | |||
@@ -10,7 +10,7 @@ use rustc_hash::FxHashMap; | |||
10 | use crate::{ | 10 | use crate::{ |
11 | SourceItemId, Path, ModuleSource, HirDatabase, Name, SourceFileItems, | 11 | SourceItemId, Path, ModuleSource, HirDatabase, Name, SourceFileItems, |
12 | HirFileId, MacroCallLoc, AsName, PerNs, DefKind, DefLoc, Function, | 12 | HirFileId, MacroCallLoc, AsName, PerNs, DefKind, DefLoc, Function, |
13 | ModuleDef, Module, Struct, Enum, | 13 | ModuleDef, Module, Struct, Enum, Const, Static, |
14 | ids::LocationCtx, | 14 | ids::LocationCtx, |
15 | }; | 15 | }; |
16 | 16 | ||
@@ -187,8 +187,22 @@ impl LoweredModule { | |||
187 | // TODO | 187 | // TODO |
188 | return; | 188 | return; |
189 | } | 189 | } |
190 | ast::ModuleItemKind::ConstDef(it) => it.name(), | 190 | ast::ModuleItemKind::ConstDef(it) => { |
191 | ast::ModuleItemKind::StaticDef(it) => it.name(), | 191 | if let Some(name) = it.name() { |
192 | let c = Const { id: ctx.to_def(it) }; | ||
193 | self.declarations | ||
194 | .insert(name.as_name(), PerNs::values(c.into())); | ||
195 | } | ||
196 | return; | ||
197 | } | ||
198 | ast::ModuleItemKind::StaticDef(it) => { | ||
199 | if let Some(name) = it.name() { | ||
200 | let s = Static { id: ctx.to_def(it) }; | ||
201 | self.declarations | ||
202 | .insert(name.as_name(), PerNs::values(s.into())); | ||
203 | } | ||
204 | return; | ||
205 | } | ||
192 | ast::ModuleItemKind::Module(_) => { | 206 | ast::ModuleItemKind::Module(_) => { |
193 | // modules are handled separately direclty by nameres | 207 | // modules are handled separately direclty by nameres |
194 | return; | 208 | return; |
@@ -246,8 +260,8 @@ impl DefKind { | |||
246 | SyntaxKind::ENUM_DEF => unreachable!(), | 260 | SyntaxKind::ENUM_DEF => unreachable!(), |
247 | SyntaxKind::TRAIT_DEF => PerNs::types(DefKind::Trait), | 261 | SyntaxKind::TRAIT_DEF => PerNs::types(DefKind::Trait), |
248 | SyntaxKind::TYPE_DEF => PerNs::types(DefKind::Type), | 262 | SyntaxKind::TYPE_DEF => PerNs::types(DefKind::Type), |
249 | SyntaxKind::CONST_DEF => PerNs::values(DefKind::Const), | 263 | SyntaxKind::CONST_DEF => unreachable!(), |
250 | SyntaxKind::STATIC_DEF => PerNs::values(DefKind::Static), | 264 | SyntaxKind::STATIC_DEF => unreachable!(), |
251 | _ => PerNs::none(), | 265 | _ => PerNs::none(), |
252 | } | 266 | } |
253 | } | 267 | } |
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 9a1a90eed..6d6150096 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs | |||
@@ -693,7 +693,10 @@ impl From<ModuleDef> for Option<TypableDef> { | |||
693 | ModuleDef::Struct(s) => s.into(), | 693 | ModuleDef::Struct(s) => s.into(), |
694 | ModuleDef::Enum(e) => e.into(), | 694 | ModuleDef::Enum(e) => e.into(), |
695 | ModuleDef::EnumVariant(v) => v.into(), | 695 | ModuleDef::EnumVariant(v) => v.into(), |
696 | ModuleDef::Def(_) | ModuleDef::Module(_) => return None, | 696 | ModuleDef::Const(_) |
697 | | ModuleDef::Static(_) | ||
698 | | ModuleDef::Def(_) | ||
699 | | ModuleDef::Module(_) => return None, | ||
697 | }; | 700 | }; |
698 | Some(res) | 701 | Some(res) |
699 | } | 702 | } |