diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-06 14:51:10 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-06 14:51:10 +0000 |
commit | cf0ce14351af03c620aca784ee2c03aad86b866e (patch) | |
tree | bffd84981df9cca1143807796dc6772ddcfe8e0b /crates/ra_hir/src/ids.rs | |
parent | eaf553dade9a28b41631387d7c88b09fd0ba64e2 (diff) | |
parent | 733383446fc229a35d4432d14c295c5a01e5a87f (diff) |
Merge #429
429: Reorganize hir public API in terms of code model r=matklad a=matklad
Recently, I've been thinking about introducing "object orient code model" API for rust: a set of APIs with types like `Function`, `Module`, etc, with methods like `get_containing_declaration()`, `get_type()`, etc.
Here's how a similar API might look like in .Net land:
https://docs.microsoft.com/en-us/dotnet/api/microsoft.codeanalysis.semanticmodel?view=roslyn-dotnet
https://docs.microsoft.com/en-us/dotnet/api/microsoft.codeanalysis.imethodsymbol?view=roslyn-dotnet
The main feature of such API is that it can be powered by different backends. For example, one can imagine a backend based on salsa, and a backend which reads all the data from a specially prepared JSON file. The "OO" bit is interesting mostly in this "can swap implementations via dynamic dispatch" aspect, the actual API could have a more database/ECS flavored feeling.
It's not clear at this moment how exactly should we implement such a dynamically (or if we even need dynamism in the first pace) swapable API in Rust, but I'd love to experiment with this a bit.
For starters, I propose creating a `code_model_api` which contains various definition types and their public methods (mandatory implemented as one-liners, so that the API has a header-file feel). Specifically, I propose that each type is a wrapper around some integer ID, and that all methods of it accept a `&db` argument. The actual impl goes elsewhere: into the db queries or, absent a better place, into the `code_model_api_impl`. In the first commit, I've moved the simplest type, `Crate`, over to this pattern.
I *think* that we, at least initially, will be used types from `code_model_api` *inside* `hir` as well, but this is not required: we might pick a different implementation down the line, while preserving the API.
Long term I'd love to replace the `db: &impl HirDatabase` argument by a `mp: &dyn ModelProvider`, implement `ModelProvider` for `T: HirDatabase`, and move `code_model_api` into the separate crate, which does not depend on `hir`.
@flodiebold you've recently done some `Def`s work, would do you think of this plan? Could it become a good API in the future, or is it just a useless boilerplate duplicating method signatures between `code_model_api` and `code_model_impl`?
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/ids.rs')
-rw-r--r-- | crates/ra_hir/src/ids.rs | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index 4d6378e02..c7391ee05 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs | |||
@@ -2,7 +2,9 @@ use ra_db::{SourceRootId, LocationIntener, Cancelable, FileId}; | |||
2 | use ra_syntax::{SourceFileNode, SyntaxKind, SyntaxNode, SyntaxNodeRef, SourceFile, AstNode, ast}; | 2 | use ra_syntax::{SourceFileNode, SyntaxKind, SyntaxNode, SyntaxNodeRef, SourceFile, AstNode, ast}; |
3 | use ra_arena::{Arena, RawId, impl_arena_id}; | 3 | use ra_arena::{Arena, RawId, impl_arena_id}; |
4 | 4 | ||
5 | use crate::{HirDatabase, PerNs, ModuleId, Module, Def, Function, Struct, Enum, ImplBlock, Crate}; | 5 | use crate::{HirDatabase, PerNs, ModuleId, Def, Function, Struct, Enum, ImplBlock, Crate}; |
6 | |||
7 | use crate::code_model_api::Module; | ||
6 | 8 | ||
7 | /// hir makes a heavy use of ids: integer (u32) handlers to various things. You | 9 | /// hir makes a heavy use of ids: integer (u32) handlers to various things. You |
8 | /// can think of id as a pointer (but without a lifetime) or a file descriptor | 10 | /// can think of id as a pointer (but without a lifetime) or a file descriptor |
@@ -151,7 +153,7 @@ impl DefId { | |||
151 | let loc = self.loc(db); | 153 | let loc = self.loc(db); |
152 | let res = match loc.kind { | 154 | let res = match loc.kind { |
153 | DefKind::Module => { | 155 | DefKind::Module => { |
154 | let module = Module::new(db, loc.source_root_id, loc.module_id)?; | 156 | let module = Module::from_module_id(db, loc.source_root_id, loc.module_id)?; |
155 | Def::Module(module) | 157 | Def::Module(module) |
156 | } | 158 | } |
157 | DefKind::Function => { | 159 | DefKind::Function => { |
@@ -175,12 +177,12 @@ impl DefId { | |||
175 | /// For a module, returns that module; for any other def, returns the containing module. | 177 | /// For a module, returns that module; for any other def, returns the containing module. |
176 | pub fn module(self, db: &impl HirDatabase) -> Cancelable<Module> { | 178 | pub fn module(self, db: &impl HirDatabase) -> Cancelable<Module> { |
177 | let loc = self.loc(db); | 179 | let loc = self.loc(db); |
178 | Module::new(db, loc.source_root_id, loc.module_id) | 180 | Module::from_module_id(db, loc.source_root_id, loc.module_id) |
179 | } | 181 | } |
180 | 182 | ||
181 | /// Returns the containing crate. | 183 | /// Returns the containing crate. |
182 | pub fn krate(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> { | 184 | pub fn krate(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> { |
183 | Ok(self.module(db)?.krate(db)) | 185 | Ok(self.module(db)?.krate(db)?) |
184 | } | 186 | } |
185 | 187 | ||
186 | /// Returns the containing impl block, if this is an impl item. | 188 | /// Returns the containing impl block, if this is an impl item. |