aboutsummaryrefslogtreecommitdiff
path: root/crates/hir/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir/src/lib.rs')
-rw-r--r--crates/hir/src/lib.rs85
1 files changed, 61 insertions, 24 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 52939e990..f0bc2c7b9 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -29,6 +29,8 @@ mod has_source;
29pub mod diagnostics; 29pub mod diagnostics;
30pub mod db; 30pub mod db;
31 31
32mod display;
33
32use std::{iter, sync::Arc}; 34use std::{iter, sync::Arc};
33 35
34use arrayvec::ArrayVec; 36use arrayvec::ArrayVec;
@@ -50,7 +52,6 @@ use hir_def::{
50use hir_expand::{diagnostics::DiagnosticSink, name::name, MacroDefKind}; 52use hir_expand::{diagnostics::DiagnosticSink, name::name, MacroDefKind};
51use hir_ty::{ 53use hir_ty::{
52 autoderef, 54 autoderef,
53 display::{write_bounds_like_dyn_trait_with_prefix, HirDisplayError, HirFormatter},
54 method_resolution::{self, TyFingerprint}, 55 method_resolution::{self, TyFingerprint},
55 primitive::UintTy, 56 primitive::UintTy,
56 to_assoc_type_id, 57 to_assoc_type_id,
@@ -572,6 +573,12 @@ impl Struct {
572 } 573 }
573} 574}
574 575
576impl HasVisibility for Struct {
577 fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
578 db.struct_data(self.id).visibility.resolve(db.upcast(), &self.id.resolver(db.upcast()))
579 }
580}
581
575#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 582#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
576pub struct Union { 583pub struct Union {
577 pub(crate) id: UnionId, 584 pub(crate) id: UnionId,
@@ -604,6 +611,12 @@ impl Union {
604 } 611 }
605} 612}
606 613
614impl HasVisibility for Union {
615 fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
616 db.union_data(self.id).visibility.resolve(db.upcast(), &self.id.resolver(db.upcast()))
617 }
618}
619
607#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 620#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
608pub struct Enum { 621pub struct Enum {
609 pub(crate) id: EnumId, 622 pub(crate) id: EnumId,
@@ -631,6 +644,12 @@ impl Enum {
631 } 644 }
632} 645}
633 646
647impl HasVisibility for Enum {
648 fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
649 db.enum_data(self.id).visibility.resolve(db.upcast(), &self.id.resolver(db.upcast()))
650 }
651}
652
634#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 653#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
635pub struct Variant { 654pub struct Variant {
636 pub(crate) parent: Enum, 655 pub(crate) parent: Enum,
@@ -822,7 +841,8 @@ impl Function {
822 db.function_data(self.id) 841 db.function_data(self.id)
823 .params 842 .params
824 .iter() 843 .iter()
825 .map(|type_ref| { 844 .enumerate()
845 .map(|(idx, type_ref)| {
826 let ty = Type { 846 let ty = Type {
827 krate, 847 krate,
828 ty: InEnvironment { 848 ty: InEnvironment {
@@ -830,7 +850,7 @@ impl Function {
830 environment: environment.clone(), 850 environment: environment.clone(),
831 }, 851 },
832 }; 852 };
833 Param { ty } 853 Param { func: self, ty, idx }
834 }) 854 })
835 .collect() 855 .collect()
836 } 856 }
@@ -844,7 +864,7 @@ impl Function {
844 } 864 }
845 865
846 pub fn is_unsafe(self, db: &dyn HirDatabase) -> bool { 866 pub fn is_unsafe(self, db: &dyn HirDatabase) -> bool {
847 db.function_data(self.id).is_unsafe 867 db.function_data(self.id).qualifier.is_unsafe
848 } 868 }
849 869
850 pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { 870 pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) {
@@ -893,6 +913,9 @@ impl From<hir_ty::Mutability> for Access {
893 913
894#[derive(Debug)] 914#[derive(Debug)]
895pub struct Param { 915pub struct Param {
916 func: Function,
917 /// The index in parameter list, including self parameter.
918 idx: usize,
896 ty: Type, 919 ty: Type,
897} 920}
898 921
@@ -900,6 +923,15 @@ impl Param {
900 pub fn ty(&self) -> &Type { 923 pub fn ty(&self) -> &Type {
901 &self.ty 924 &self.ty
902 } 925 }
926
927 pub fn pattern_source(&self, db: &dyn HirDatabase) -> Option<ast::Pat> {
928 let params = self.func.source(db)?.value.param_list()?;
929 if params.self_param().is_some() {
930 params.params().nth(self.idx.checked_sub(1)?)?.pat()
931 } else {
932 params.params().nth(self.idx)?.pat()
933 }
934 }
903} 935}
904 936
905#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 937#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -922,6 +954,14 @@ impl SelfParam {
922 }) 954 })
923 .unwrap_or(Access::Owned) 955 .unwrap_or(Access::Owned)
924 } 956 }
957
958 pub fn display(self, db: &dyn HirDatabase) -> &'static str {
959 match self.access(db) {
960 Access::Shared => "&self",
961 Access::Exclusive => "&mut self",
962 Access::Owned => "self",
963 }
964 }
925} 965}
926 966
927impl HasVisibility for Function { 967impl HasVisibility for Function {
@@ -949,6 +989,10 @@ impl Const {
949 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { 989 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
950 db.const_data(self.id).name.clone() 990 db.const_data(self.id).name.clone()
951 } 991 }
992
993 pub fn type_ref(self, db: &dyn HirDatabase) -> TypeRef {
994 db.const_data(self.id).type_ref.clone()
995 }
952} 996}
953 997
954impl HasVisibility for Const { 998impl HasVisibility for Const {
@@ -982,6 +1026,12 @@ impl Static {
982 } 1026 }
983} 1027}
984 1028
1029impl HasVisibility for Static {
1030 fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
1031 db.static_data(self.id).visibility.resolve(db.upcast(), &self.id.resolver(db.upcast()))
1032 }
1033}
1034
985#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 1035#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
986pub struct Trait { 1036pub struct Trait {
987 pub(crate) id: TraitId, 1037 pub(crate) id: TraitId,
@@ -1001,7 +1051,13 @@ impl Trait {
1001 } 1051 }
1002 1052
1003 pub fn is_auto(self, db: &dyn HirDatabase) -> bool { 1053 pub fn is_auto(self, db: &dyn HirDatabase) -> bool {
1004 db.trait_data(self.id).auto 1054 db.trait_data(self.id).is_auto
1055 }
1056}
1057
1058impl HasVisibility for Trait {
1059 fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
1060 db.trait_data(self.id).visibility.resolve(db.upcast(), &self.id.resolver(db.upcast()))
1005 } 1061 }
1006} 1062}
1007 1063
@@ -1413,19 +1469,6 @@ impl TypeParam {
1413 } 1469 }
1414} 1470}
1415 1471
1416impl HirDisplay for TypeParam {
1417 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
1418 write!(f, "{}", self.name(f.db))?;
1419 let bounds = f.db.generic_predicates_for_param(self.id);
1420 let substs = Substs::type_params(f.db, self.id.parent);
1421 let predicates = bounds.iter().cloned().map(|b| b.subst(&substs)).collect::<Vec<_>>();
1422 if !(predicates.is_empty() || f.omit_verbose_types()) {
1423 write_bounds_like_dyn_trait_with_prefix(":", &predicates, f)?;
1424 }
1425 Ok(())
1426 }
1427}
1428
1429#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] 1472#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
1430pub struct LifetimeParam { 1473pub struct LifetimeParam {
1431 pub(crate) id: LifetimeParamId, 1474 pub(crate) id: LifetimeParamId,
@@ -2059,12 +2102,6 @@ impl Type {
2059 } 2102 }
2060} 2103}
2061 2104
2062impl HirDisplay for Type {
2063 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
2064 self.ty.value.hir_fmt(f)
2065 }
2066}
2067
2068// FIXME: closures 2105// FIXME: closures
2069#[derive(Debug)] 2106#[derive(Debug)]
2070pub struct Callable { 2107pub struct Callable {