aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model_api.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/code_model_api.rs')
-rw-r--r--crates/ra_hir/src/code_model_api.rs65
1 files changed, 64 insertions, 1 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index f06f1ae66..902032e14 100644
--- a/crates/ra_hir/src/code_model_api.rs
+++ b/crates/ra_hir/src/code_model_api.rs
@@ -5,10 +5,12 @@ use ra_db::{CrateId, Cancelable, FileId};
5use ra_syntax::{ast, TreePtr, SyntaxNode}; 5use ra_syntax::{ast, TreePtr, SyntaxNode};
6 6
7use crate::{ 7use crate::{
8 Name, DefId, Path, PerNs, 8 Name, DefId, Path, PerNs, ScopesWithSyntaxMapping,
9 type_ref::TypeRef, 9 type_ref::TypeRef,
10 nameres::ModuleScope, 10 nameres::ModuleScope,
11 db::HirDatabase, 11 db::HirDatabase,
12 expr::BodySyntaxMapping,
13 ty::InferenceResult,
12}; 14};
13 15
14/// hir::Crate describes a single crate. It's the main inteface with which 16/// hir::Crate describes a single crate. It's the main inteface with which
@@ -37,6 +39,14 @@ impl Crate {
37 } 39 }
38} 40}
39 41
42pub enum Def {
43 Module(Module),
44 Struct(Struct),
45 Enum(Enum),
46 Function(Function),
47 Item,
48}
49
40#[derive(Debug, Clone, PartialEq, Eq, Hash)] 50#[derive(Debug, Clone, PartialEq, Eq, Hash)]
41pub struct Module { 51pub struct Module {
42 pub(crate) def_id: DefId, 52 pub(crate) def_id: DefId,
@@ -207,3 +217,56 @@ impl Enum {
207 Ok(db.enum_data(self.def_id)?.variants.clone()) 217 Ok(db.enum_data(self.def_id)?.variants.clone())
208 } 218 }
209} 219}
220
221#[derive(Debug, Clone, PartialEq, Eq, Hash)]
222pub struct Function {
223 pub(crate) def_id: DefId,
224}
225
226/// The declared signature of a function.
227#[derive(Debug, Clone, PartialEq, Eq)]
228pub struct FnSignature {
229 pub(crate) args: Vec<TypeRef>,
230 pub(crate) ret_type: TypeRef,
231}
232
233impl 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
243impl 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}