mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-28 04:45:05 +00:00
Hook up query, add impls, lower moar
This commit is contained in:
parent
1d75d11d6c
commit
34bc80650b
3 changed files with 128 additions and 41 deletions
|
@ -3,7 +3,6 @@
|
||||||
|
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use hir_expand::{
|
use hir_expand::{
|
||||||
db::AstDatabase,
|
|
||||||
hygiene::Hygiene,
|
hygiene::Hygiene,
|
||||||
name::{name, AsName, Name},
|
name::{name, AsName, Name},
|
||||||
HirFileId, MacroDefId, MacroDefKind,
|
HirFileId, MacroDefId, MacroDefKind,
|
||||||
|
@ -42,8 +41,8 @@ pub(crate) struct LowerCtx {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LowerCtx {
|
impl LowerCtx {
|
||||||
pub fn new(db: &dyn AstDatabase, file_id: HirFileId) -> Self {
|
pub fn new(db: &dyn DefDatabase, file_id: HirFileId) -> Self {
|
||||||
LowerCtx { hygiene: Hygiene::new(db, file_id) }
|
LowerCtx { hygiene: Hygiene::new(db.upcast(), file_id) }
|
||||||
}
|
}
|
||||||
pub fn with_hygiene(hygiene: &Hygiene) -> Self {
|
pub fn with_hygiene(hygiene: &Hygiene) -> Self {
|
||||||
LowerCtx { hygiene: hygiene.clone() }
|
LowerCtx { hygiene: hygiene.clone() }
|
||||||
|
@ -120,7 +119,7 @@ impl ExprCollector<'_> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ctx(&self) -> LowerCtx {
|
fn ctx(&self) -> LowerCtx {
|
||||||
LowerCtx::new(self.db.upcast(), self.expander.current_file_id)
|
LowerCtx::new(self.db, self.expander.current_file_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId {
|
fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId {
|
||||||
|
|
|
@ -14,6 +14,7 @@ use crate::{
|
||||||
docs::Documentation,
|
docs::Documentation,
|
||||||
generics::GenericParams,
|
generics::GenericParams,
|
||||||
import_map::ImportMap,
|
import_map::ImportMap,
|
||||||
|
item_tree::ItemTree,
|
||||||
lang_item::{LangItemTarget, LangItems},
|
lang_item::{LangItemTarget, LangItems},
|
||||||
nameres::{raw::RawItems, CrateDefMap},
|
nameres::{raw::RawItems, CrateDefMap},
|
||||||
AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, FunctionId, FunctionLoc,
|
AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, FunctionId, FunctionLoc,
|
||||||
|
@ -48,6 +49,9 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> {
|
||||||
#[salsa::invoke(RawItems::raw_items_query)]
|
#[salsa::invoke(RawItems::raw_items_query)]
|
||||||
fn raw_items(&self, file_id: HirFileId) -> Arc<RawItems>;
|
fn raw_items(&self, file_id: HirFileId) -> Arc<RawItems>;
|
||||||
|
|
||||||
|
#[salsa::invoke(ItemTree::item_tree_query)]
|
||||||
|
fn item_tree(&self, file_id: HirFileId) -> Arc<ItemTree>;
|
||||||
|
|
||||||
#[salsa::invoke(crate_def_map_wait)]
|
#[salsa::invoke(crate_def_map_wait)]
|
||||||
#[salsa::transparent]
|
#[salsa::transparent]
|
||||||
fn crate_def_map(&self, krate: CrateId) -> Arc<CrateDefMap>;
|
fn crate_def_map(&self, krate: CrateId) -> Arc<CrateDefMap>;
|
||||||
|
|
|
@ -4,25 +4,28 @@ use hir_expand::{
|
||||||
ast_id_map::{AstIdMap, FileAstId},
|
ast_id_map::{AstIdMap, FileAstId},
|
||||||
hygiene::Hygiene,
|
hygiene::Hygiene,
|
||||||
name::{name, AsName, Name},
|
name::{name, AsName, Name},
|
||||||
|
HirFileId,
|
||||||
};
|
};
|
||||||
use ra_arena::{Arena, Idx, RawId};
|
use ra_arena::{Arena, Idx, RawId};
|
||||||
use ra_syntax::ast;
|
use ra_syntax::{ast, match_ast};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
attr::Attrs,
|
attr::Attrs,
|
||||||
|
db::DefDatabase,
|
||||||
generics::GenericParams,
|
generics::GenericParams,
|
||||||
path::{path, AssociatedTypeBinding, GenericArgs, ImportAlias, ModPath, Path},
|
path::{path, AssociatedTypeBinding, GenericArgs, ImportAlias, ModPath, Path},
|
||||||
type_ref::{Mutability, TypeBound, TypeRef},
|
type_ref::{Mutability, TypeBound, TypeRef},
|
||||||
visibility::RawVisibility,
|
visibility::RawVisibility,
|
||||||
};
|
};
|
||||||
use ast::{NameOwner, StructKind, TypeAscriptionOwner};
|
use ast::{AstNode, ModuleItemOwner, NameOwner, StructKind, TypeAscriptionOwner};
|
||||||
use std::{
|
use std::{
|
||||||
ops::{Index, Range},
|
ops::{Index, Range},
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Debug, Default, Eq, PartialEq)]
|
||||||
pub struct ItemTree {
|
pub struct ItemTree {
|
||||||
|
top_level: Vec<ModItem>,
|
||||||
imports: Arena<Import>,
|
imports: Arena<Import>,
|
||||||
functions: Arena<Function>,
|
functions: Arena<Function>,
|
||||||
structs: Arena<Struct>,
|
structs: Arena<Struct>,
|
||||||
|
@ -41,8 +44,42 @@ pub struct ItemTree {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ItemTree {
|
impl ItemTree {
|
||||||
pub fn query(syntax: &ast::SourceFile) -> ItemTree {
|
pub fn item_tree_query(db: &dyn DefDatabase, file_id: HirFileId) -> Arc<ItemTree> {
|
||||||
todo!()
|
let syntax = if let Some(node) = db.parse_or_expand(file_id) {
|
||||||
|
node
|
||||||
|
} else {
|
||||||
|
return Default::default();
|
||||||
|
};
|
||||||
|
|
||||||
|
let (macro_storage, file_storage);
|
||||||
|
let item_owner = match_ast! {
|
||||||
|
match syntax {
|
||||||
|
ast::MacroItems(items) => {
|
||||||
|
macro_storage = items;
|
||||||
|
¯o_storage as &dyn ModuleItemOwner
|
||||||
|
},
|
||||||
|
ast::SourceFile(file) => {
|
||||||
|
file_storage = file;
|
||||||
|
&file_storage
|
||||||
|
},
|
||||||
|
_ => return Default::default(),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let map = db.ast_id_map(file_id);
|
||||||
|
let ctx = Ctx {
|
||||||
|
tree: ItemTree::default(),
|
||||||
|
hygiene: Hygiene::new(db.upcast(), file_id),
|
||||||
|
source_ast_id_map: map,
|
||||||
|
body_ctx: crate::body::LowerCtx::new(db, file_id),
|
||||||
|
};
|
||||||
|
Arc::new(ctx.lower(item_owner))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns an iterator over all items located at the top level of the `HirFileId` this
|
||||||
|
/// `ItemTree` was created from.
|
||||||
|
pub fn top_level_items(&self) -> impl Iterator<Item = ModItem> + '_ {
|
||||||
|
self.top_level.iter().copied()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,6 +115,7 @@ impl_index!(
|
||||||
exprs: Expr,
|
exprs: Expr,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct Import {
|
pub struct Import {
|
||||||
pub path: ModPath,
|
pub path: ModPath,
|
||||||
pub alias: Option<ImportAlias>,
|
pub alias: Option<ImportAlias>,
|
||||||
|
@ -88,6 +126,7 @@ pub struct Import {
|
||||||
pub is_macro_use: bool,
|
pub is_macro_use: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct Function {
|
pub struct Function {
|
||||||
pub name: Name,
|
pub name: Name,
|
||||||
pub attrs: Attrs,
|
pub attrs: Attrs,
|
||||||
|
@ -99,6 +138,7 @@ pub struct Function {
|
||||||
pub ast: FileAstId<ast::FnDef>,
|
pub ast: FileAstId<ast::FnDef>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct Struct {
|
pub struct Struct {
|
||||||
pub name: Name,
|
pub name: Name,
|
||||||
pub attrs: Attrs,
|
pub attrs: Attrs,
|
||||||
|
@ -108,6 +148,7 @@ pub struct Struct {
|
||||||
pub ast: FileAstId<ast::StructDef>,
|
pub ast: FileAstId<ast::StructDef>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct Union {
|
pub struct Union {
|
||||||
pub name: Name,
|
pub name: Name,
|
||||||
pub attrs: Attrs,
|
pub attrs: Attrs,
|
||||||
|
@ -116,6 +157,7 @@ pub struct Union {
|
||||||
pub fields: Fields,
|
pub fields: Fields,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct Enum {
|
pub struct Enum {
|
||||||
pub name: Name,
|
pub name: Name,
|
||||||
pub attrs: Attrs,
|
pub attrs: Attrs,
|
||||||
|
@ -124,6 +166,7 @@ pub struct Enum {
|
||||||
pub variants: Range<Idx<Variant>>,
|
pub variants: Range<Idx<Variant>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct Const {
|
pub struct Const {
|
||||||
/// const _: () = ();
|
/// const _: () = ();
|
||||||
pub name: Option<Name>,
|
pub name: Option<Name>,
|
||||||
|
@ -131,12 +174,14 @@ pub struct Const {
|
||||||
pub type_ref: TypeRef,
|
pub type_ref: TypeRef,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct Static {
|
pub struct Static {
|
||||||
pub name: Name,
|
pub name: Name,
|
||||||
pub visibility: RawVisibility,
|
pub visibility: RawVisibility,
|
||||||
pub type_ref: TypeRef,
|
pub type_ref: TypeRef,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct Trait {
|
pub struct Trait {
|
||||||
pub name: Name,
|
pub name: Name,
|
||||||
pub visibility: RawVisibility,
|
pub visibility: RawVisibility,
|
||||||
|
@ -145,6 +190,7 @@ pub struct Trait {
|
||||||
pub items: Vec<AssocItem>,
|
pub items: Vec<AssocItem>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct Impl {
|
pub struct Impl {
|
||||||
pub generic_params: GenericParams,
|
pub generic_params: GenericParams,
|
||||||
pub target_trait: Option<TypeRef>,
|
pub target_trait: Option<TypeRef>,
|
||||||
|
@ -161,12 +207,14 @@ pub struct TypeAlias {
|
||||||
pub type_ref: Option<TypeRef>,
|
pub type_ref: Option<TypeRef>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct Mod {
|
pub struct Mod {
|
||||||
pub name: Name,
|
pub name: Name,
|
||||||
pub visibility: RawVisibility,
|
pub visibility: RawVisibility,
|
||||||
pub items: Vec<ModItem>,
|
pub items: Vec<ModItem>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct MacroCall {
|
pub struct MacroCall {
|
||||||
pub name: Option<Name>,
|
pub name: Option<Name>,
|
||||||
pub path: ModPath,
|
pub path: ModPath,
|
||||||
|
@ -177,8 +225,22 @@ pub struct MacroCall {
|
||||||
|
|
||||||
// NB: There's no `FileAstId` for `Expr`. The only case where this would be useful is for array
|
// NB: There's no `FileAstId` for `Expr`. The only case where this would be useful is for array
|
||||||
// lengths, but we don't do much with them yet.
|
// lengths, but we don't do much with them yet.
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct Expr;
|
pub struct Expr;
|
||||||
|
|
||||||
|
macro_rules! impl_froms {
|
||||||
|
($e:ident { $($v:ident ($t:ty)),* $(,)? }) => {
|
||||||
|
$(
|
||||||
|
impl From<$t> for $e {
|
||||||
|
fn from(it: $t) -> $e {
|
||||||
|
$e::$v(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||||
pub enum ModItem {
|
pub enum ModItem {
|
||||||
Import(Idx<Import>),
|
Import(Idx<Import>),
|
||||||
Function(Idx<Function>),
|
Function(Idx<Function>),
|
||||||
|
@ -194,6 +256,22 @@ pub enum ModItem {
|
||||||
MacroCall(Idx<MacroCall>),
|
MacroCall(Idx<MacroCall>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl_froms!(ModItem {
|
||||||
|
Import(Idx<Import>),
|
||||||
|
Function(Idx<Function>),
|
||||||
|
Struct(Idx<Struct>),
|
||||||
|
Union(Idx<Union>),
|
||||||
|
Enum(Idx<Enum>),
|
||||||
|
Const(Idx<Const>),
|
||||||
|
Static(Idx<Static>),
|
||||||
|
Trait(Idx<Trait>),
|
||||||
|
Impl(Idx<Impl>),
|
||||||
|
TypeAlias(Idx<TypeAlias>),
|
||||||
|
Mod(Idx<Mod>),
|
||||||
|
MacroCall(Idx<MacroCall>),
|
||||||
|
});
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub enum AssocItem {
|
pub enum AssocItem {
|
||||||
Function(Idx<Function>),
|
Function(Idx<Function>),
|
||||||
TypeAlias(Idx<TypeAlias>),
|
TypeAlias(Idx<TypeAlias>),
|
||||||
|
@ -201,6 +279,14 @@ pub enum AssocItem {
|
||||||
MacroCall(Idx<MacroCall>),
|
MacroCall(Idx<MacroCall>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl_froms!(AssocItem {
|
||||||
|
Function(Idx<Function>),
|
||||||
|
TypeAlias(Idx<TypeAlias>),
|
||||||
|
Const(Idx<Const>),
|
||||||
|
MacroCall(Idx<MacroCall>),
|
||||||
|
});
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct Variant {
|
pub struct Variant {
|
||||||
pub name: Name,
|
pub name: Name,
|
||||||
pub fields: Fields,
|
pub fields: Fields,
|
||||||
|
@ -229,55 +315,44 @@ struct Ctx {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Ctx {
|
impl Ctx {
|
||||||
fn lower(&mut self, item_owner: &dyn ast::ModuleItemOwner) {
|
fn lower(mut self, item_owner: &dyn ModuleItemOwner) -> ItemTree {
|
||||||
for item in item_owner.items() {
|
self.tree.top_level = item_owner.items().flat_map(|item| self.lower_item(&item)).collect();
|
||||||
self.lower_item(&item)
|
self.tree
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lower_item(&mut self, item: &ast::ModuleItem) {
|
fn lower_item(&mut self, item: &ast::ModuleItem) -> Option<ModItem> {
|
||||||
match item {
|
match item {
|
||||||
ast::ModuleItem::StructDef(ast) => {
|
ast::ModuleItem::StructDef(ast) => {
|
||||||
if let Some(data) = self.lower_struct(ast) {
|
self.lower_struct(ast).map(|data| self.tree.structs.alloc(data).into())
|
||||||
let idx = self.tree.structs.alloc(data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ast::ModuleItem::UnionDef(ast) => {
|
ast::ModuleItem::UnionDef(ast) => {
|
||||||
if let Some(data) = self.lower_union(ast) {
|
self.lower_union(ast).map(|data| self.tree.unions.alloc(data).into())
|
||||||
let idx = self.tree.unions.alloc(data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ast::ModuleItem::EnumDef(ast) => {
|
ast::ModuleItem::EnumDef(ast) => {
|
||||||
if let Some(data) = self.lower_enum(ast) {
|
self.lower_enum(ast).map(|data| self.tree.enums.alloc(data).into())
|
||||||
let idx = self.tree.enums.alloc(data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ast::ModuleItem::FnDef(ast) => {
|
ast::ModuleItem::FnDef(ast) => {
|
||||||
if let Some(data) = self.lower_function(ast) {
|
self.lower_function(ast).map(|data| self.tree.functions.alloc(data).into())
|
||||||
let idx = self.tree.functions.alloc(data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ast::ModuleItem::TypeAliasDef(ast) => {
|
ast::ModuleItem::TypeAliasDef(ast) => {
|
||||||
if let Some(data) = self.lower_type_alias(ast) {
|
self.lower_type_alias(ast).map(|data| self.tree.type_aliases.alloc(data).into())
|
||||||
let idx = self.tree.type_aliases.alloc(data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ast::ModuleItem::StaticDef(ast) => {
|
ast::ModuleItem::StaticDef(ast) => {
|
||||||
if let Some(data) = self.lower_static(ast) {
|
self.lower_static(ast).map(|data| self.tree.statics.alloc(data).into())
|
||||||
let idx = self.tree.statics.alloc(data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ast::ModuleItem::ConstDef(ast) => {
|
ast::ModuleItem::ConstDef(ast) => {
|
||||||
let data = self.lower_const(ast);
|
let data = self.lower_const(ast);
|
||||||
let idx = self.tree.consts.alloc(data);
|
Some(self.tree.consts.alloc(data).into())
|
||||||
}
|
}
|
||||||
ast::ModuleItem::Module(_) => {}
|
ast::ModuleItem::Module(ast) => {
|
||||||
ast::ModuleItem::TraitDef(_) => {}
|
self.lower_module(ast).map(|data| self.tree.mods.alloc(data).into())
|
||||||
ast::ModuleItem::ImplDef(_) => {}
|
}
|
||||||
ast::ModuleItem::UseItem(_) => {}
|
ast::ModuleItem::TraitDef(_) => todo!(),
|
||||||
ast::ModuleItem::ExternCrateItem(_) => {}
|
ast::ModuleItem::ImplDef(_) => todo!(),
|
||||||
ast::ModuleItem::MacroCall(_) => {}
|
ast::ModuleItem::UseItem(_) => todo!(),
|
||||||
ast::ModuleItem::ExternBlock(_) => {}
|
ast::ModuleItem::ExternCrateItem(_) => todo!(),
|
||||||
|
ast::ModuleItem::MacroCall(_) => todo!(),
|
||||||
|
ast::ModuleItem::ExternBlock(_) => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -473,7 +548,16 @@ impl Ctx {
|
||||||
Const { name, visibility, type_ref }
|
Const { name, visibility, type_ref }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lower_generic_params(&mut self, item: &impl ast::TypeParamsOwner) -> GenericParams {
|
fn lower_module(&mut self, module: &ast::Module) -> Option<Mod> {
|
||||||
|
let name = module.name()?.as_name();
|
||||||
|
let visibility = self.lower_visibility(module);
|
||||||
|
let items = module
|
||||||
|
.item_list()
|
||||||
|
.map(move |list| list.items().flat_map(move |item| self.lower_item(&item)).collect());
|
||||||
|
Some(Mod { name, visibility, items: items.unwrap_or_default() })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn lower_generic_params(&mut self, _item: &impl ast::TypeParamsOwner) -> GenericParams {
|
||||||
None.unwrap()
|
None.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue