aboutsummaryrefslogtreecommitdiff
path: root/crates/hir
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir')
-rw-r--r--crates/hir/src/lib.rs71
1 files changed, 51 insertions, 20 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 30b96d7e2..eb7865c84 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -154,11 +154,7 @@ impl Crate {
154 } 154 }
155 155
156 pub fn transitive_reverse_dependencies(self, db: &dyn HirDatabase) -> Vec<Crate> { 156 pub fn transitive_reverse_dependencies(self, db: &dyn HirDatabase) -> Vec<Crate> {
157 db.crate_graph() 157 db.crate_graph().transitive_rev_deps(self.id).into_iter().map(|id| Crate { id }).collect()
158 .transitive_reverse_dependencies(self.id)
159 .into_iter()
160 .map(|id| Crate { id })
161 .collect()
162 } 158 }
163 159
164 pub fn root_module(self, db: &dyn HirDatabase) -> Module { 160 pub fn root_module(self, db: &dyn HirDatabase) -> Module {
@@ -856,6 +852,7 @@ impl Function {
856 }) 852 })
857 .collect() 853 .collect()
858 } 854 }
855
859 pub fn method_params(self, db: &dyn HirDatabase) -> Option<Vec<Param>> { 856 pub fn method_params(self, db: &dyn HirDatabase) -> Option<Vec<Param>> {
860 if self.self_param(db).is_none() { 857 if self.self_param(db).is_none() {
861 return None; 858 return None;
@@ -913,7 +910,7 @@ impl From<hir_ty::Mutability> for Access {
913 } 910 }
914} 911}
915 912
916#[derive(Debug)] 913#[derive(Clone, Debug)]
917pub struct Param { 914pub struct Param {
918 func: Function, 915 func: Function,
919 /// The index in parameter list, including self parameter. 916 /// The index in parameter list, including self parameter.
@@ -926,13 +923,25 @@ impl Param {
926 &self.ty 923 &self.ty
927 } 924 }
928 925
926 pub fn as_local(&self, db: &dyn HirDatabase) -> Local {
927 let parent = DefWithBodyId::FunctionId(self.func.into());
928 let body = db.body(parent);
929 Local { parent, pat_id: body.params[self.idx] }
930 }
931
929 pub fn pattern_source(&self, db: &dyn HirDatabase) -> Option<ast::Pat> { 932 pub fn pattern_source(&self, db: &dyn HirDatabase) -> Option<ast::Pat> {
930 let params = self.func.source(db)?.value.param_list()?; 933 self.source(db).and_then(|p| p.value.pat())
934 }
935
936 pub fn source(&self, db: &dyn HirDatabase) -> Option<InFile<ast::Param>> {
937 let InFile { file_id, value } = self.func.source(db)?;
938 let params = value.param_list()?;
931 if params.self_param().is_some() { 939 if params.self_param().is_some() {
932 params.params().nth(self.idx.checked_sub(1)?)?.pat() 940 params.params().nth(self.idx.checked_sub(1)?)
933 } else { 941 } else {
934 params.params().nth(self.idx)?.pat() 942 params.params().nth(self.idx)
935 } 943 }
944 .map(|value| InFile { file_id, value })
936 } 945 }
937} 946}
938 947
@@ -964,6 +973,14 @@ impl SelfParam {
964 Access::Owned => "self", 973 Access::Owned => "self",
965 } 974 }
966 } 975 }
976
977 pub fn source(&self, db: &dyn HirDatabase) -> Option<InFile<ast::SelfParam>> {
978 let InFile { file_id, value } = Function::from(self.func).source(db)?;
979 value
980 .param_list()
981 .and_then(|params| params.self_param())
982 .map(|value| InFile { file_id, value })
983 }
967} 984}
968 985
969impl HasVisibility for Function { 986impl HasVisibility for Function {
@@ -1121,6 +1138,14 @@ impl BuiltinType {
1121} 1138}
1122 1139
1123#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 1140#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1141pub enum MacroKind {
1142 Declarative,
1143 ProcMacro,
1144 Derive,
1145 BuiltIn,
1146}
1147
1148#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1124pub struct MacroDef { 1149pub struct MacroDef {
1125 pub(crate) id: MacroDefId, 1150 pub(crate) id: MacroDefId,
1126} 1151}
@@ -1144,15 +1169,15 @@ impl MacroDef {
1144 } 1169 }
1145 } 1170 }
1146 1171
1147 /// Indicate it is a proc-macro 1172 pub fn kind(&self) -> MacroKind {
1148 pub fn is_proc_macro(&self) -> bool { 1173 match self.id.kind {
1149 matches!(self.id.kind, MacroDefKind::ProcMacro(..)) 1174 MacroDefKind::Declarative(_) => MacroKind::Declarative,
1150 } 1175 MacroDefKind::BuiltIn(_, _) => MacroKind::BuiltIn,
1151 1176 MacroDefKind::BuiltInDerive(_, _) => MacroKind::Derive,
1152 /// Indicate it is a derive macro 1177 MacroDefKind::BuiltInEager(_, _) => MacroKind::BuiltIn,
1153 pub fn is_derive_macro(&self) -> bool { 1178 // FIXME might be a derive
1154 // FIXME: wrong for `ProcMacro` 1179 MacroDefKind::ProcMacro(_, _) => MacroKind::ProcMacro,
1155 matches!(self.id.kind, MacroDefKind::ProcMacro(..) | MacroDefKind::BuiltInDerive(..)) 1180 }
1156 } 1181 }
1157} 1182}
1158 1183
@@ -1331,6 +1356,13 @@ impl Local {
1331 } 1356 }
1332 } 1357 }
1333 1358
1359 pub fn as_self_param(self, db: &dyn HirDatabase) -> Option<SelfParam> {
1360 match self.parent {
1361 DefWithBodyId::FunctionId(func) if self.is_self(db) => Some(SelfParam { func }),
1362 _ => None,
1363 }
1364 }
1365
1334 // FIXME: why is this an option? It shouldn't be? 1366 // FIXME: why is this an option? It shouldn't be?
1335 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { 1367 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
1336 let body = db.body(self.parent); 1368 let body = db.body(self.parent);
@@ -1572,8 +1604,7 @@ impl Impl {
1572 pub fn all_for_trait(db: &dyn HirDatabase, trait_: Trait) -> Vec<Impl> { 1604 pub fn all_for_trait(db: &dyn HirDatabase, trait_: Trait) -> Vec<Impl> {
1573 let krate = trait_.module(db).krate(); 1605 let krate = trait_.module(db).krate();
1574 let mut all = Vec::new(); 1606 let mut all = Vec::new();
1575 for Crate { id } in krate.transitive_reverse_dependencies(db).into_iter().chain(Some(krate)) 1607 for Crate { id } in krate.transitive_reverse_dependencies(db).into_iter() {
1576 {
1577 let impls = db.trait_impls_in_crate(id); 1608 let impls = db.trait_impls_in_crate(id);
1578 all.extend(impls.for_trait(trait_.id).map(Self::from)) 1609 all.extend(impls.for_trait(trait_.id).map(Self::from))
1579 } 1610 }