From cb642fc578100c0945088accb85acb8f03d2e1fd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 Nov 2019 20:33:18 +0300 Subject: Move generic_params query to HIR --- crates/ra_hir_def/src/db.rs | 6 +++++- crates/ra_hir_def/src/generics.rs | 29 ++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) (limited to 'crates/ra_hir_def/src') diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs index c6cd4369b..844f8bbe8 100644 --- a/crates/ra_hir_def/src/db.rs +++ b/crates/ra_hir_def/src/db.rs @@ -8,13 +8,14 @@ use ra_syntax::ast; use crate::{ adt::{EnumData, StructData}, body::{scope::ExprScopes, Body, BodySourceMap}, + generics::GenericParams, impls::ImplData, nameres::{ raw::{ImportSourceMap, RawItems}, CrateDefMap, }, traits::TraitData, - DefWithBodyId, EnumId, ImplId, ItemLoc, StructOrUnionId, TraitId, + DefWithBodyId, EnumId, GenericDefId, ImplId, ItemLoc, StructOrUnionId, TraitId, }; #[salsa::query_group(InternDatabaseStorage)] @@ -71,4 +72,7 @@ pub trait DefDatabase2: InternDatabase + AstDatabase { #[salsa::invoke(ExprScopes::expr_scopes_query)] fn expr_scopes(&self, def: DefWithBodyId) -> Arc; + + #[salsa::invoke(GenericParams::generic_params_query)] + fn generic_params(&self, def: GenericDefId) -> Arc; } diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs index 17a5d5f43..f794eefbc 100644 --- a/crates/ra_hir_def/src/generics.rs +++ b/crates/ra_hir_def/src/generics.rs @@ -5,13 +5,12 @@ use std::sync::Arc; use hir_expand::name::{self, AsName, Name}; - use ra_syntax::ast::{self, NameOwner, TypeBoundsOwner, TypeParamsOwner}; use crate::{ db::DefDatabase2, type_ref::{TypeBound, TypeRef}, - AdtId, AstItemDef, GenericDefId, HasSource, Lookup, + AdtId, AstItemDef, ContainerId, GenericDefId, HasSource, Lookup, }; /// Data about a generic parameter (to a function, struct, impl, ...). @@ -43,7 +42,15 @@ pub struct WherePredicate { } impl GenericParams { - pub fn new( + pub(crate) fn generic_params_query( + db: &impl DefDatabase2, + def: GenericDefId, + ) -> Arc { + let parent_generics = parent_generic_def(db, def).map(|it| db.generic_params(it)); + Arc::new(GenericParams::new(db, def.into(), parent_generics)) + } + + fn new( db: &impl DefDatabase2, def: GenericDefId, parent_params: Option>, @@ -161,3 +168,19 @@ impl GenericParams { vec } } + +fn parent_generic_def(db: &impl DefDatabase2, def: GenericDefId) -> Option { + let container = match def { + GenericDefId::FunctionId(it) => it.lookup(db).container, + GenericDefId::TypeAliasId(it) => it.lookup(db).container, + GenericDefId::ConstId(it) => it.lookup(db).container, + GenericDefId::EnumVariantId(it) => return Some(it.parent.into()), + GenericDefId::AdtId(_) | GenericDefId::TraitId(_) | GenericDefId::ImplId(_) => return None, + }; + + match container { + ContainerId::ImplId(it) => Some(it.into()), + ContainerId::TraitId(it) => Some(it.into()), + ContainerId::ModuleId(_) => None, + } +} -- cgit v1.2.3