Support for nested ADT

This commit is contained in:
Aleksey Kladov 2019-12-20 12:20:49 +01:00
parent 94ad07af4b
commit ac5a3f611b
5 changed files with 35 additions and 22 deletions

View file

@ -269,7 +269,7 @@ pub struct Struct {
impl Struct { impl Struct {
pub fn module(self, db: &impl DefDatabase) -> Module { pub fn module(self, db: &impl DefDatabase) -> Module {
Module { id: self.id.lookup(db).container } Module { id: self.id.lookup(db).container.module(db) }
} }
pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
@ -290,7 +290,7 @@ impl Struct {
} }
pub fn ty(self, db: &impl HirDatabase) -> Type { pub fn ty(self, db: &impl HirDatabase) -> Type {
Type::from_def(db, self.id.lookup(db).container.krate, self.id) Type::from_def(db, self.id.lookup(db).container.module(db).krate, self.id)
} }
fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> { fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
@ -309,11 +309,11 @@ impl Union {
} }
pub fn module(self, db: &impl DefDatabase) -> Module { pub fn module(self, db: &impl DefDatabase) -> Module {
Module { id: self.id.lookup(db).container } Module { id: self.id.lookup(db).container.module(db) }
} }
pub fn ty(self, db: &impl HirDatabase) -> Type { pub fn ty(self, db: &impl HirDatabase) -> Type {
Type::from_def(db, self.id.lookup(db).container.krate, self.id) Type::from_def(db, self.id.lookup(db).container.module(db).krate, self.id)
} }
pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> { pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> {
@ -337,7 +337,7 @@ pub struct Enum {
impl Enum { impl Enum {
pub fn module(self, db: &impl DefDatabase) -> Module { pub fn module(self, db: &impl DefDatabase) -> Module {
Module { id: self.id.lookup(db).container } Module { id: self.id.lookup(db).container.module(db) }
} }
pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
@ -357,7 +357,7 @@ impl Enum {
} }
pub fn ty(self, db: &impl HirDatabase) -> Type { pub fn ty(self, db: &impl HirDatabase) -> Type {
Type::from_def(db, self.id.lookup(db).container.krate, self.id) Type::from_def(db, self.id.lookup(db).container.module(db).krate, self.id)
} }
} }

View file

@ -25,7 +25,7 @@ use crate::{
path::GenericArgs, path::GenericArgs,
path::Path, path::Path,
type_ref::{Mutability, TypeRef}, type_ref::{Mutability, TypeRef},
ContainerId, DefWithBodyId, FunctionLoc, Intern, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, ModuleDefId, StructLoc, UnionLoc,
}; };
pub(super) fn lower( pub(super) fn lower(
@ -490,16 +490,28 @@ where
} }
fn collect_block_items(&mut self, block: &ast::Block) { fn collect_block_items(&mut self, block: &ast::Block) {
let container = ContainerId::DefWithBodyId(self.def).into(); let container = ContainerId::DefWithBodyId(self.def);
for item in block.items() { for item in block.items() {
match item { let def: ModuleDefId = match item {
ast::ModuleItem::FnDef(def) => { ast::ModuleItem::FnDef(def) => {
let ast_id = self.expander.ast_id(&def); let ast_id = self.expander.ast_id(&def);
self.body.defs.push(FunctionLoc { container, ast_id }.intern(self.db).into()) FunctionLoc { container: container.into(), ast_id }.intern(self.db).into()
} }
// FIXME: handle other items ast::ModuleItem::StructDef(def) => {
_ => (), let ast_id = self.expander.ast_id(&def);
} StructLoc { container, ast_id }.intern(self.db).into()
}
ast::ModuleItem::EnumDef(def) => {
let ast_id = self.expander.ast_id(&def);
EnumLoc { container, ast_id }.intern(self.db).into()
}
ast::ModuleItem::UnionDef(def) => {
let ast_id = self.expander.ast_id(&def);
UnionLoc { container, ast_id }.intern(self.db).into()
}
_ => continue,
};
self.body.defs.push(def)
} }
} }

View file

@ -95,7 +95,7 @@ impl_intern_key!(StructId);
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct StructLoc { pub struct StructLoc {
pub container: ModuleId, pub container: ContainerId,
pub ast_id: AstId<ast::StructDef>, pub ast_id: AstId<ast::StructDef>,
} }
@ -119,7 +119,7 @@ impl_intern_key!(UnionId);
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct UnionLoc { pub struct UnionLoc {
pub container: ModuleId, pub container: ContainerId,
pub ast_id: AstId<ast::UnionDef>, pub ast_id: AstId<ast::UnionDef>,
} }
@ -143,7 +143,7 @@ impl_intern_key!(EnumId);
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EnumLoc { pub struct EnumLoc {
pub container: ModuleId, pub container: ContainerId,
pub ast_id: AstId<ast::EnumDef>, pub ast_id: AstId<ast::EnumDef>,
} }
@ -529,6 +529,7 @@ impl HasModule for AdtId {
AdtId::UnionId(it) => it.lookup(db).container, AdtId::UnionId(it) => it.lookup(db).container,
AdtId::EnumId(it) => it.lookup(db).container, AdtId::EnumId(it) => it.lookup(db).container,
} }
.module(db)
} }
} }
@ -550,7 +551,7 @@ impl HasModule for GenericDefId {
GenericDefId::TraitId(it) => it.lookup(db).container, GenericDefId::TraitId(it) => it.lookup(db).container,
GenericDefId::TypeAliasId(it) => it.lookup(db).module(db), GenericDefId::TypeAliasId(it) => it.lookup(db).module(db),
GenericDefId::ImplId(it) => it.lookup(db).container, GenericDefId::ImplId(it) => it.lookup(db).container,
GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container, GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container.module(db),
GenericDefId::ConstId(it) => it.lookup(db).module(db), GenericDefId::ConstId(it) => it.lookup(db).module(db),
} }
} }

View file

@ -772,17 +772,17 @@ where
PerNs::values(def.into()) PerNs::values(def.into())
} }
raw::DefKind::Struct(ast_id) => { raw::DefKind::Struct(ast_id) => {
let def = StructLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) } let def = StructLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
.intern(self.def_collector.db); .intern(self.def_collector.db);
PerNs::both(def.into(), def.into()) PerNs::both(def.into(), def.into())
} }
raw::DefKind::Union(ast_id) => { raw::DefKind::Union(ast_id) => {
let def = UnionLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) } let def = UnionLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
.intern(self.def_collector.db); .intern(self.def_collector.db);
PerNs::both(def.into(), def.into()) PerNs::both(def.into(), def.into())
} }
raw::DefKind::Enum(ast_id) => { raw::DefKind::Enum(ast_id) => {
let def = EnumLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) } let def = EnumLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
.intern(self.def_collector.db); .intern(self.def_collector.db);
PerNs::types(def.into()) PerNs::types(def.into())
} }

View file

@ -697,8 +697,8 @@ impl CallableDef {
pub fn krate(self, db: &impl HirDatabase) -> CrateId { pub fn krate(self, db: &impl HirDatabase) -> CrateId {
match self { match self {
CallableDef::FunctionId(f) => f.lookup(db).module(db), CallableDef::FunctionId(f) => f.lookup(db).module(db),
CallableDef::StructId(s) => s.lookup(db).container, CallableDef::StructId(s) => s.lookup(db).container.module(db),
CallableDef::EnumVariantId(e) => e.parent.lookup(db).container, CallableDef::EnumVariantId(e) => e.parent.lookup(db).container.module(db),
} }
.krate .krate
} }