mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 04:19:13 +00:00
Added defWithBody
This commit is contained in:
parent
2a770190b0
commit
7f3bf7cc73
7 changed files with 120 additions and 24 deletions
|
@ -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)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub struct Function {
|
pub struct Function {
|
||||||
pub(crate) id: FunctionId,
|
pub(crate) id: FunctionId,
|
||||||
|
@ -483,11 +555,11 @@ impl Function {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
|
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> {
|
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 {
|
pub fn ty(&self, db: &impl HirDatabase) -> Ty {
|
||||||
|
@ -495,8 +567,8 @@ impl Function {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap {
|
pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap {
|
||||||
let scopes = db.expr_scopes(*self);
|
let scopes = db.expr_scopes( DefWithBody::Func(*self));
|
||||||
let source_map = db.body_with_source_map(*self).1;
|
let source_map = db.body_with_source_map(DefWithBody::Func(*self)).1;
|
||||||
ScopesWithSourceMap { scopes, source_map }
|
ScopesWithSourceMap { scopes, source_map }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -505,7 +577,7 @@ impl Function {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> {
|
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> {
|
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> {
|
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
|
||||||
docs_from_ast(&*self.source(db).1)
|
docs_from_ast(&*self.source(db).1)
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -8,6 +8,7 @@ use crate::{
|
||||||
Function, FnSignature, ExprScopes, TypeAlias,
|
Function, FnSignature, ExprScopes, TypeAlias,
|
||||||
Struct, Enum, StructField,
|
Struct, Enum, StructField,
|
||||||
Const, ConstSignature, Static,
|
Const, ConstSignature, Static,
|
||||||
|
DefWithBody,
|
||||||
nameres::{Namespace, ImportSourceMap, RawItems, CrateDefMap},
|
nameres::{Namespace, ImportSourceMap, RawItems, CrateDefMap},
|
||||||
ty::{InferenceResult, Ty, method_resolution::CrateImplBlocks, TypableDef, CallableDef, FnSig},
|
ty::{InferenceResult, Ty, method_resolution::CrateImplBlocks, TypableDef, CallableDef, FnSig},
|
||||||
adt::{StructData, EnumData},
|
adt::{StructData, EnumData},
|
||||||
|
@ -83,10 +84,10 @@ pub trait DefDatabase: SourceDatabase + AsRef<HirInterner> {
|
||||||
#[salsa::query_group(HirDatabaseStorage)]
|
#[salsa::query_group(HirDatabaseStorage)]
|
||||||
pub trait HirDatabase: DefDatabase {
|
pub trait HirDatabase: DefDatabase {
|
||||||
#[salsa::invoke(ExprScopes::expr_scopes_query)]
|
#[salsa::invoke(ExprScopes::expr_scopes_query)]
|
||||||
fn expr_scopes(&self, func: Function) -> Arc<ExprScopes>;
|
fn expr_scopes(&self, def: DefWithBody) -> Arc<ExprScopes>;
|
||||||
|
|
||||||
#[salsa::invoke(crate::ty::infer)]
|
#[salsa::invoke(crate::ty::infer)]
|
||||||
fn infer(&self, func: Function) -> Arc<InferenceResult>;
|
fn infer(&self, def:DefWithBody) -> Arc<InferenceResult>;
|
||||||
|
|
||||||
#[salsa::invoke(crate::ty::type_for_def)]
|
#[salsa::invoke(crate::ty::type_for_def)]
|
||||||
fn type_for_def(&self, def: TypableDef, ns: Namespace) -> Ty;
|
fn type_for_def(&self, def: TypableDef, ns: Namespace) -> Ty;
|
||||||
|
@ -100,11 +101,11 @@ pub trait HirDatabase: DefDatabase {
|
||||||
#[salsa::invoke(crate::expr::body_with_source_map_query)]
|
#[salsa::invoke(crate::expr::body_with_source_map_query)]
|
||||||
fn body_with_source_map(
|
fn body_with_source_map(
|
||||||
&self,
|
&self,
|
||||||
func: Function,
|
def: DefWithBody,
|
||||||
) -> (Arc<crate::expr::Body>, Arc<crate::expr::BodySourceMap>);
|
) -> (Arc<crate::expr::Body>, Arc<crate::expr::BodySourceMap>);
|
||||||
|
|
||||||
#[salsa::invoke(crate::expr::body_hir_query)]
|
#[salsa::invoke(crate::expr::body_hir_query)]
|
||||||
fn body_hir(&self, func: Function) -> Arc<crate::expr::Body>;
|
fn body_hir(&self, def: DefWithBody) -> Arc<crate::expr::Body>;
|
||||||
|
|
||||||
#[salsa::invoke(crate::ty::method_resolution::CrateImplBlocks::impls_in_crate_query)]
|
#[salsa::invoke(crate::ty::method_resolution::CrateImplBlocks::impls_in_crate_query)]
|
||||||
fn impls_in_crate(&self, krate: Crate) -> Arc<CrateImplBlocks>;
|
fn impls_in_crate(&self, krate: Crate) -> Arc<CrateImplBlocks>;
|
||||||
|
|
|
@ -10,7 +10,7 @@ use ra_syntax::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Path, Name, HirDatabase, Function, Resolver,
|
Path, Name, HirDatabase, Function, Resolver,DefWithBody,
|
||||||
name::AsName,
|
name::AsName,
|
||||||
type_ref::{Mutability, TypeRef},
|
type_ref::{Mutability, TypeRef},
|
||||||
};
|
};
|
||||||
|
@ -29,7 +29,7 @@ impl_arena_id!(ExprId);
|
||||||
pub struct Body {
|
pub struct Body {
|
||||||
// FIXME: this should be more general, consts & statics also have bodies
|
// FIXME: this should be more general, consts & statics also have bodies
|
||||||
/// The Function of the item this body belongs to
|
/// The Function of the item this body belongs to
|
||||||
owner: Function,
|
owner: DefWithBody,
|
||||||
exprs: Arena<ExprId, Expr>,
|
exprs: Arena<ExprId, Expr>,
|
||||||
pats: Arena<PatId, Pat>,
|
pats: Arena<PatId, Pat>,
|
||||||
/// The patterns for the function's parameters. While the parameter types are
|
/// The patterns for the function's parameters. While the parameter types are
|
||||||
|
@ -66,7 +66,7 @@ impl Body {
|
||||||
self.body_expr
|
self.body_expr
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn owner(&self) -> Function {
|
pub fn owner(&self) -> DefWithBody {
|
||||||
self.owner
|
self.owner
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -464,7 +464,7 @@ impl Pat {
|
||||||
// Queries
|
// Queries
|
||||||
|
|
||||||
struct ExprCollector {
|
struct ExprCollector {
|
||||||
owner: Function,
|
owner: DefWithBody,
|
||||||
exprs: Arena<ExprId, Expr>,
|
exprs: Arena<ExprId, Expr>,
|
||||||
pats: Arena<PatId, Pat>,
|
pats: Arena<PatId, Pat>,
|
||||||
source_map: BodySourceMap,
|
source_map: BodySourceMap,
|
||||||
|
@ -473,7 +473,7 @@ struct ExprCollector {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ExprCollector {
|
impl ExprCollector {
|
||||||
fn new(owner: Function) -> Self {
|
fn new(owner: DefWithBody) -> Self {
|
||||||
ExprCollector {
|
ExprCollector {
|
||||||
owner,
|
owner,
|
||||||
exprs: Arena::default(),
|
exprs: Arena::default(),
|
||||||
|
@ -503,6 +503,9 @@ impl ExprCollector {
|
||||||
self.exprs.alloc(block)
|
self.exprs.alloc(block)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
fn collect_expr(&mut self, expr: &ast::Expr) -> ExprId {
|
fn collect_expr(&mut self, expr: &ast::Expr) -> ExprId {
|
||||||
let syntax_ptr = SyntaxNodePtr::new(expr.syntax());
|
let syntax_ptr = SyntaxNodePtr::new(expr.syntax());
|
||||||
match expr.kind() {
|
match expr.kind() {
|
||||||
|
@ -871,6 +874,15 @@ impl ExprCollector {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn collect_const_body(&mut self,node:&ast::ConstDef) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn collect_static_body(&mut self,node:&ast::StaticDef) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
fn collect_fn_body(&mut self, node: &ast::FnDef) {
|
fn collect_fn_body(&mut self, node: &ast::FnDef) {
|
||||||
if let Some(param_list) = node.param_list() {
|
if let Some(param_list) = node.param_list() {
|
||||||
if let Some(self_param) = param_list.self_param() {
|
if let Some(self_param) = param_list.self_param() {
|
||||||
|
@ -917,19 +929,25 @@ impl ExprCollector {
|
||||||
|
|
||||||
pub(crate) fn body_with_source_map_query(
|
pub(crate) fn body_with_source_map_query(
|
||||||
db: &impl HirDatabase,
|
db: &impl HirDatabase,
|
||||||
func: Function,
|
def: DefWithBody,
|
||||||
) -> (Arc<Body>, Arc<BodySourceMap>) {
|
) -> (Arc<Body>, Arc<BodySourceMap>) {
|
||||||
let mut collector = ExprCollector::new(func);
|
|
||||||
|
|
||||||
// FIXME: consts, etc.
|
let mut collector = ExprCollector::new(def);
|
||||||
collector.collect_fn_body(&func.source(db).1);
|
|
||||||
|
|
||||||
|
// FIXME: do can this be turned into a method
|
||||||
|
|
||||||
|
match def {
|
||||||
|
DefWithBody::Const(ref c) => collector.collect_const_body(&def.const_source(db).1),
|
||||||
|
DefWithBody::Func(ref f) => collector.collect_fn_body(&def.func_source(db).1),
|
||||||
|
DefWithBody::Static(ref s) => collector.collect_static_body(&def.static_source(db).1)
|
||||||
|
}
|
||||||
|
|
||||||
let (body, source_map) = collector.finish();
|
let (body, source_map) = collector.finish();
|
||||||
(Arc::new(body), Arc::new(source_map))
|
(Arc::new(body), Arc::new(source_map))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn body_hir_query(db: &impl HirDatabase, func: Function) -> Arc<Body> {
|
pub(crate) fn body_hir_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<Body> {
|
||||||
db.body_with_source_map(func).0
|
db.body_with_source_map(def).0
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
@ -10,7 +10,7 @@ use ra_syntax::{
|
||||||
use ra_arena::{Arena, RawId, impl_arena_id};
|
use ra_arena::{Arena, RawId, impl_arena_id};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Name, AsName, Function,
|
Name, AsName, Function,DefWithBody,
|
||||||
expr::{PatId, ExprId, Pat, Expr, Body, Statement, BodySourceMap},
|
expr::{PatId, ExprId, Pat, Expr, Body, Statement, BodySourceMap},
|
||||||
HirDatabase,
|
HirDatabase,
|
||||||
};
|
};
|
||||||
|
@ -40,8 +40,8 @@ pub struct ScopeData {
|
||||||
|
|
||||||
impl ExprScopes {
|
impl ExprScopes {
|
||||||
// FIXME: This should take something more general than Function
|
// FIXME: This should take something more general than Function
|
||||||
pub(crate) fn expr_scopes_query(db: &impl HirDatabase, function: Function) -> Arc<ExprScopes> {
|
pub(crate) fn expr_scopes_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<ExprScopes> {
|
||||||
let body = db.body_hir(function);
|
let body = db.body_hir(def);
|
||||||
let res = ExprScopes::new(body);
|
let res = ExprScopes::new(body);
|
||||||
Arc::new(res)
|
Arc::new(res)
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,10 +67,12 @@ pub use self::{
|
||||||
|
|
||||||
pub use self::code_model_api::{
|
pub use self::code_model_api::{
|
||||||
Crate, CrateDependency,
|
Crate, CrateDependency,
|
||||||
|
DefWithBody,
|
||||||
Module, ModuleDef, ModuleSource,
|
Module, ModuleDef, ModuleSource,
|
||||||
Struct, Enum, EnumVariant,
|
Struct, Enum, EnumVariant,
|
||||||
Function, FnSignature,
|
Function, FnSignature,
|
||||||
StructField, FieldSource,
|
StructField, FieldSource,
|
||||||
Static, Const, ConstSignature,
|
Static, Const, ConstSignature,
|
||||||
Trait, TypeAlias,
|
Trait, TypeAlias,
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -657,6 +657,7 @@ impl ToOwned for ContinueExpr {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
impl ContinueExpr {}
|
impl ContinueExpr {}
|
||||||
|
|
||||||
// DynTraitType
|
// DynTraitType
|
||||||
|
|
|
@ -313,6 +313,7 @@ Grammar(
|
||||||
"DocCommentsOwner",
|
"DocCommentsOwner",
|
||||||
"TypeAscriptionOwner",
|
"TypeAscriptionOwner",
|
||||||
],
|
],
|
||||||
|
options: ["body","Block"],
|
||||||
),
|
),
|
||||||
"StaticDef": (
|
"StaticDef": (
|
||||||
traits: [
|
traits: [
|
||||||
|
@ -323,6 +324,7 @@ Grammar(
|
||||||
"DocCommentsOwner",
|
"DocCommentsOwner",
|
||||||
"TypeAscriptionOwner",
|
"TypeAscriptionOwner",
|
||||||
],
|
],
|
||||||
|
options: ["body","Block"],
|
||||||
),
|
),
|
||||||
"TypeAliasDef": (
|
"TypeAliasDef": (
|
||||||
traits: [
|
traits: [
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue