Added defWithBody

This commit is contained in:
Lenard Pratt 2019-03-30 10:50:00 +00:00
parent 2a770190b0
commit 7f3bf7cc73
7 changed files with 120 additions and 24 deletions

View file

@ -433,6 +433,78 @@ impl Docs for EnumVariant {
}
}
/// The defs which have a body.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DefWithBody {
Func(Function),
Const(Const),
Static(Static),
}
impl DefWithBody {
pub fn get_funct(&self) -> &Function {
match *self {
DefWithBody::Func(ref f) => f,
_ => unreachable!()
}
}
pub fn const_source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::ConstDef>) {
match *self {
DefWithBody::Const(ref c) => c.source(db),
_ => unreachable!()
}
}
pub fn func_source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::FnDef>) {
match *self {
DefWithBody::Func(ref f) => f.source(db),
_ => unreachable!()
}
}
pub fn static_source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::StaticDef>) {
match *self {
DefWithBody::Static(ref s) => s.source(db),
_ => unreachable!()
}
}
pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> {
db.infer(*self)
}
pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> {
db.body_hir(*self)
}
/// Builds a resolver for code inside this item.
pub fn resolver(&self, db: &impl HirDatabase) -> Resolver {
// // take the outer scope...
// let r = self
// .impl_block(db)
// .map(|ib| ib.resolver(db))
// .unwrap_or_else(|| self.module(db).resolver(db));
// // ...and add generic params, if present
// let p = self.generic_params(db);
// let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
// r
unimplemented!()
}
pub fn signature(&self, db: &impl HirDatabase) -> Arc<FnSignature> {
// db.fn_signature(*self)
unimplemented!()
}
pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap {
let scopes = db.expr_scopes(*self);
let source_map = db.body_with_source_map(*self).1;
ScopesWithSourceMap { scopes, source_map }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Function {
pub(crate) id: FunctionId,
@ -483,11 +555,11 @@ impl Function {
}
pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
db.body_with_source_map(*self).1
db.body_with_source_map(DefWithBody::Func(*self)).1
}
pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> {
db.body_hir(*self)
db.body_hir(DefWithBody::Func(*self))
}
pub fn ty(&self, db: &impl HirDatabase) -> Ty {
@ -495,8 +567,8 @@ impl Function {
}
pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap {
let scopes = db.expr_scopes(*self);
let source_map = db.body_with_source_map(*self).1;
let scopes = db.expr_scopes( DefWithBody::Func(*self));
let source_map = db.body_with_source_map(DefWithBody::Func(*self)).1;
ScopesWithSourceMap { scopes, source_map }
}
@ -505,7 +577,7 @@ impl Function {
}
pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> {
db.infer(*self)
db.infer(DefWithBody::Func(*self))
}
pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> {
@ -716,4 +788,4 @@ impl Docs for TypeAlias {
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
docs_from_ast(&*self.source(db).1)
}
}
}