Skip to content

Commit 1c25bf0

Browse files
bors[bot]matklad
andcommitted
467: move function to code_model_api r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 3b166ae + ac92973 commit 1c25bf0

File tree

9 files changed

+153
-144
lines changed

9 files changed

+153
-144
lines changed

crates/ra_hir/src/code_model_api.rs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ use ra_db::{CrateId, Cancelable, FileId};
55
use ra_syntax::{ast, TreePtr, SyntaxNode};
66

77
use crate::{
8-
Name, DefId, Path, PerNs,
8+
Name, DefId, Path, PerNs, ScopesWithSyntaxMapping,
99
type_ref::TypeRef,
1010
nameres::ModuleScope,
1111
db::HirDatabase,
12+
expr::BodySyntaxMapping,
13+
ty::InferenceResult,
1214
};
1315

1416
/// hir::Crate describes a single crate. It's the main inteface with which
@@ -37,6 +39,14 @@ impl Crate {
3739
}
3840
}
3941

42+
pub enum Def {
43+
Module(Module),
44+
Struct(Struct),
45+
Enum(Enum),
46+
Function(Function),
47+
Item,
48+
}
49+
4050
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4151
pub struct Module {
4252
pub(crate) def_id: DefId,
@@ -207,3 +217,56 @@ impl Enum {
207217
Ok(db.enum_data(self.def_id)?.variants.clone())
208218
}
209219
}
220+
221+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
222+
pub struct Function {
223+
pub(crate) def_id: DefId,
224+
}
225+
226+
/// The declared signature of a function.
227+
#[derive(Debug, Clone, PartialEq, Eq)]
228+
pub struct FnSignature {
229+
pub(crate) args: Vec<TypeRef>,
230+
pub(crate) ret_type: TypeRef,
231+
}
232+
233+
impl FnSignature {
234+
pub fn args(&self) -> &[TypeRef] {
235+
&self.args
236+
}
237+
238+
pub fn ret_type(&self) -> &TypeRef {
239+
&self.ret_type
240+
}
241+
}
242+
243+
impl Function {
244+
pub fn def_id(&self) -> DefId {
245+
self.def_id
246+
}
247+
248+
pub fn source(&self, db: &impl HirDatabase) -> TreePtr<ast::FnDef> {
249+
self.source_impl(db)
250+
}
251+
252+
pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Cancelable<Arc<BodySyntaxMapping>> {
253+
db.body_syntax_mapping(self.def_id)
254+
}
255+
256+
pub fn scopes(&self, db: &impl HirDatabase) -> Cancelable<ScopesWithSyntaxMapping> {
257+
let scopes = db.fn_scopes(self.def_id)?;
258+
let syntax_mapping = db.body_syntax_mapping(self.def_id)?;
259+
Ok(ScopesWithSyntaxMapping {
260+
scopes,
261+
syntax_mapping,
262+
})
263+
}
264+
265+
pub fn signature(&self, db: &impl HirDatabase) -> Arc<FnSignature> {
266+
db.fn_signature(self.def_id)
267+
}
268+
269+
pub fn infer(&self, db: &impl HirDatabase) -> Cancelable<Arc<InferenceResult>> {
270+
db.infer(self.def_id)
271+
}
272+
}

crates/ra_hir/src/code_model_impl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
mod krate; // `crate` is invalid ident :(
22
mod module;
3+
pub(crate) mod function;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
mod scope;
2+
3+
use std::sync::Arc;
4+
5+
use ra_db::Cancelable;
6+
use ra_syntax::{
7+
TreePtr,
8+
ast::{self, AstNode},
9+
};
10+
11+
use crate::{
12+
DefId, DefKind, HirDatabase, Name, Function, FnSignature, Module,
13+
type_ref::{TypeRef, Mutability},
14+
expr::Body,
15+
impl_block::ImplBlock,
16+
};
17+
18+
pub use self::scope::{FnScopes, ScopesWithSyntaxMapping};
19+
20+
impl Function {
21+
pub(crate) fn new(def_id: DefId) -> Function {
22+
Function { def_id }
23+
}
24+
25+
pub(crate) fn source_impl(&self, db: &impl HirDatabase) -> TreePtr<ast::FnDef> {
26+
let def_loc = self.def_id.loc(db);
27+
assert!(def_loc.kind == DefKind::Function);
28+
let syntax = db.file_item(def_loc.source_item_id);
29+
ast::FnDef::cast(&syntax).unwrap().to_owned()
30+
}
31+
32+
pub(crate) fn body(&self, db: &impl HirDatabase) -> Cancelable<Arc<Body>> {
33+
db.body_hir(self.def_id)
34+
}
35+
36+
pub(crate) fn module(&self, db: &impl HirDatabase) -> Cancelable<Module> {
37+
self.def_id.module(db)
38+
}
39+
40+
/// The containing impl block, if this is a method.
41+
pub(crate) fn impl_block(&self, db: &impl HirDatabase) -> Cancelable<Option<ImplBlock>> {
42+
self.def_id.impl_block(db)
43+
}
44+
}
45+
46+
impl FnSignature {
47+
pub(crate) fn fn_signature_query(db: &impl HirDatabase, def_id: DefId) -> Arc<FnSignature> {
48+
let func = Function::new(def_id);
49+
let node = func.source(db);
50+
let mut args = Vec::new();
51+
if let Some(param_list) = node.param_list() {
52+
if let Some(self_param) = param_list.self_param() {
53+
let self_type = if let Some(type_ref) = self_param.type_ref() {
54+
TypeRef::from_ast(type_ref)
55+
} else {
56+
let self_type = TypeRef::Path(Name::self_type().into());
57+
match self_param.flavor() {
58+
ast::SelfParamFlavor::Owned => self_type,
59+
ast::SelfParamFlavor::Ref => {
60+
TypeRef::Reference(Box::new(self_type), Mutability::Shared)
61+
}
62+
ast::SelfParamFlavor::MutRef => {
63+
TypeRef::Reference(Box::new(self_type), Mutability::Mut)
64+
}
65+
}
66+
};
67+
args.push(self_type);
68+
}
69+
for param in param_list.params() {
70+
let type_ref = TypeRef::from_ast_opt(param.type_ref());
71+
args.push(type_ref);
72+
}
73+
}
74+
let ret_type = if let Some(type_ref) = node.ret_type().and_then(|rt| rt.type_ref()) {
75+
TypeRef::from_ast(type_ref)
76+
} else {
77+
TypeRef::unit()
78+
};
79+
let sig = FnSignature { args, ret_type };
80+
Arc::new(sig)
81+
}
82+
}

crates/ra_hir/src/db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ pub trait HirDatabase: SyntaxDatabase
106106

107107
fn fn_signature(def_id: DefId) -> Arc<FnSignature> {
108108
type FnSignatureQuery;
109-
use fn crate::function::fn_signature;
109+
use fn crate::FnSignature::fn_signature_query;
110110
}
111111
}
112112

crates/ra_hir/src/expr.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -758,10 +758,7 @@ pub(crate) fn body_syntax_mapping(
758758
let def = def_id.resolve(db)?;
759759

760760
let body_syntax_mapping = match def {
761-
Def::Function(f) => {
762-
let node = f.syntax(db);
763-
collect_fn_body_syntax(&node)
764-
}
761+
Def::Function(f) => collect_fn_body_syntax(&f.source(db)),
765762
// TODO: consts, etc.
766763
_ => panic!("Trying to get body for item type without body"),
767764
};

crates/ra_hir/src/function.rs

Lines changed: 0 additions & 126 deletions
This file was deleted.

crates/ra_hir/src/lib.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ mod macros;
2626
mod name;
2727
mod module_tree;
2828
mod nameres;
29-
mod function;
3029
mod adt;
3130
mod type_ref;
3231
mod ty;
@@ -48,21 +47,15 @@ pub use self::{
4847
ids::{HirFileId, DefId, DefLoc, MacroCallId, MacroCallLoc},
4948
macros::{MacroDef, MacroInput, MacroExpansion},
5049
nameres::{ItemMap, PerNs, Namespace, Resolution},
51-
function::{Function, FnSignature, FnScopes, ScopesWithSyntaxMapping},
5250
ty::Ty,
5351
impl_block::{ImplBlock, ImplItem},
52+
code_model_impl::function::{FnScopes, ScopesWithSyntaxMapping},
5453
};
5554

5655
pub use self::code_model_api::{
5756
Crate, CrateDependency,
57+
Def,
5858
Module, ModuleSource, Problem,
5959
Struct, Enum, VariantData, StructField,
60+
Function, FnSignature,
6061
};
61-
62-
pub enum Def {
63-
Module(Module),
64-
Function(Function),
65-
Struct(Struct),
66-
Enum(Enum),
67-
Item,
68-
}

crates/ra_hir/src/query_definitions.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ use ra_db::{SourceRootId, Cancelable,};
1212

1313
use crate::{
1414
SourceFileItems, SourceItemId, DefId, HirFileId, ModuleSource,
15-
MacroCallLoc,
15+
MacroCallLoc, FnScopes,
1616
db::HirDatabase,
17-
function::FnScopes,
1817
module_tree::ModuleId,
1918
nameres::{InputModuleItems, ItemMap, Resolver},
2019
};

0 commit comments

Comments
 (0)