rust-analyzer/crates/ra_hir/src/adt.rs

218 lines
6.8 KiB
Rust
Raw Normal View History

//! This module contains the implementation details of the HIR for ADTs, i.e.
//! structs and enums (and unions).
use std::sync::Arc;
use ra_arena::{impl_arena_id, Arena, RawId};
2019-07-19 07:43:01 +00:00
use ra_syntax::ast::{self, NameOwner, StructKind, TypeAscriptionOwner};
use crate::{
2019-09-08 06:53:49 +00:00
db::{AstDatabase, DefDatabase, HirDatabase},
type_ref::TypeRef,
AsName, Enum, EnumVariant, FieldSource, HasSource, Name, Source, Struct, StructField,
};
impl Struct {
2019-07-05 16:02:32 +00:00
pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
db.struct_data(self).variant_data.clone()
2019-01-09 15:46:02 +00:00
}
}
2019-05-23 17:18:47 +00:00
/// Note that we use `StructData` for unions as well!
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StructData {
2019-01-08 12:19:37 +00:00
pub(crate) name: Option<Name>,
pub(crate) variant_data: Arc<VariantData>,
}
impl StructData {
2019-01-08 12:38:29 +00:00
fn new(struct_def: &ast::StructDef) -> StructData {
2018-12-28 18:34:58 +00:00
let name = struct_def.name().map(|n| n.as_name());
2019-04-02 09:48:14 +00:00
let variant_data = VariantData::new(struct_def.kind());
let variant_data = Arc::new(variant_data);
StructData { name, variant_data }
}
2019-01-08 12:38:29 +00:00
2019-06-01 18:17:57 +00:00
pub(crate) fn struct_data_query(
db: &(impl DefDatabase + AstDatabase),
struct_: Struct,
) -> Arc<StructData> {
2019-06-10 23:34:34 +00:00
let src = struct_.source(db);
2019-07-19 07:43:01 +00:00
Arc::new(StructData::new(&src.ast))
2019-01-08 12:38:29 +00:00
}
}
2019-07-19 07:43:01 +00:00
fn variants(enum_def: &ast::EnumDef) -> impl Iterator<Item = ast::EnumVariant> {
2019-02-08 11:49:43 +00:00
enum_def.variant_list().into_iter().flat_map(|it| it.variants())
2019-01-25 08:35:38 +00:00
}
impl EnumVariant {
2019-01-25 09:41:23 +00:00
pub(crate) fn source_impl(
2019-07-05 16:02:32 +00:00
self,
2019-06-01 18:17:57 +00:00
db: &(impl DefDatabase + AstDatabase),
2019-07-19 07:43:01 +00:00
) -> Source<ast::EnumVariant> {
2019-06-11 00:17:29 +00:00
let src = self.parent.source(db);
2019-07-19 07:43:01 +00:00
let ast = variants(&src.ast)
2019-01-25 09:41:23 +00:00
.zip(db.enum_data(self.parent).variants.iter())
.find(|(_syntax, (id, _))| *id == self.id)
2019-01-25 08:35:38 +00:00
.unwrap()
2019-09-25 01:32:01 +00:00
.0;
2019-06-11 00:17:29 +00:00
Source { file_id: src.file_id, ast }
2019-01-25 08:35:38 +00:00
}
2019-07-05 16:02:32 +00:00
pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
2019-02-08 11:49:43 +00:00
db.enum_data(self.parent).variants[self.id].variant_data.clone()
2019-01-25 11:21:14 +00:00
}
2019-01-25 08:35:38 +00:00
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EnumData {
2019-01-08 12:22:57 +00:00
pub(crate) name: Option<Name>,
2019-01-25 09:41:23 +00:00
pub(crate) variants: Arena<EnumVariantId, EnumVariantData>,
}
impl EnumData {
2019-06-01 18:17:57 +00:00
pub(crate) fn enum_data_query(db: &(impl DefDatabase + AstDatabase), e: Enum) -> Arc<EnumData> {
2019-06-11 00:17:29 +00:00
let src = e.source(db);
let name = src.ast.name().map(|n| n.as_name());
2019-07-19 07:43:01 +00:00
let variants = variants(&src.ast)
2019-01-25 11:21:14 +00:00
.map(|var| EnumVariantData {
2019-01-25 09:41:23 +00:00
name: var.name().map(|it| it.as_name()),
2019-04-02 09:48:14 +00:00
variant_data: Arc::new(VariantData::new(var.kind())),
2019-01-25 11:21:14 +00:00
})
.collect();
Arc::new(EnumData { name, variants })
}
}
2019-01-25 11:21:14 +00:00
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct EnumVariantId(RawId);
impl_arena_id!(EnumVariantId);
#[derive(Debug, Clone, PartialEq, Eq)]
2019-01-25 11:25:30 +00:00
pub(crate) struct EnumVariantData {
pub(crate) name: Option<Name>,
2019-01-25 11:25:30 +00:00
variant_data: Arc<VariantData>,
}
2019-01-25 11:21:14 +00:00
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct StructFieldId(RawId);
impl_arena_id!(StructFieldId);
2019-01-09 15:46:02 +00:00
/// A single field of an enum variant or struct
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StructFieldData {
2019-01-09 15:46:02 +00:00
pub(crate) name: Name,
pub(crate) type_ref: TypeRef,
}
/// Fields of an enum variant or struct
#[derive(Debug, Clone, PartialEq, Eq)]
2019-01-25 11:25:30 +00:00
pub(crate) struct VariantData(VariantDataInner);
2019-01-25 11:21:14 +00:00
#[derive(Debug, Clone, PartialEq, Eq)]
enum VariantDataInner {
Struct(Arena<StructFieldId, StructFieldData>),
Tuple(Arena<StructFieldId, StructFieldData>),
2019-01-09 15:46:02 +00:00
Unit,
}
impl VariantData {
2019-01-25 11:21:14 +00:00
pub(crate) fn fields(&self) -> Option<&Arena<StructFieldId, StructFieldData>> {
match &self.0 {
VariantDataInner::Struct(fields) | VariantDataInner::Tuple(fields) => Some(fields),
_ => None,
2019-01-09 15:46:02 +00:00
}
}
}
impl VariantData {
2019-04-02 09:48:14 +00:00
fn new(flavor: StructKind) -> Self {
2019-01-25 11:21:14 +00:00
let inner = match flavor {
2019-04-02 09:48:14 +00:00
ast::StructKind::Tuple(fl) => {
let fields = fl
.fields()
.enumerate()
.map(|(i, fd)| StructFieldData {
2018-12-28 18:34:58 +00:00
name: Name::tuple_field_name(i),
type_ref: TypeRef::from_ast_opt(fd.type_ref()),
})
.collect();
2019-01-25 11:21:14 +00:00
VariantDataInner::Tuple(fields)
}
2019-04-02 09:48:14 +00:00
ast::StructKind::Named(fl) => {
let fields = fl
.fields()
.map(|fd| StructFieldData {
2018-12-28 18:34:58 +00:00
name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing),
type_ref: TypeRef::from_ast_opt(fd.ascribed_type()),
})
.collect();
2019-01-25 11:21:14 +00:00
VariantDataInner::Struct(fields)
}
2019-04-02 09:48:14 +00:00
ast::StructKind::Unit => VariantDataInner::Unit,
2019-01-25 11:21:14 +00:00
};
VariantData(inner)
}
}
2019-01-25 17:32:34 +00:00
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum VariantDef {
Struct(Struct),
EnumVariant(EnumVariant),
}
impl_froms!(VariantDef: Struct, EnumVariant);
impl VariantDef {
2019-07-12 16:56:18 +00:00
pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> {
match self {
VariantDef::Struct(it) => it.fields(db),
VariantDef::EnumVariant(it) => it.fields(db),
}
}
2019-01-25 17:32:34 +00:00
pub(crate) fn field(self, db: &impl HirDatabase, name: &Name) -> Option<StructField> {
match self {
VariantDef::Struct(it) => it.field(db, name),
VariantDef::EnumVariant(it) => it.field(db, name),
}
}
pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
2019-01-25 17:32:34 +00:00
match self {
VariantDef::Struct(it) => it.variant_data(db),
VariantDef::EnumVariant(it) => it.variant_data(db),
}
}
}
impl StructField {
2019-06-11 15:07:42 +00:00
pub(crate) fn source_impl(&self, db: &(impl DefDatabase + AstDatabase)) -> Source<FieldSource> {
2019-01-25 17:32:34 +00:00
let var_data = self.parent.variant_data(db);
let fields = var_data.fields().unwrap();
let ss;
let es;
2019-04-02 09:48:14 +00:00
let (file_id, struct_kind) = match self.parent {
2019-01-25 17:32:34 +00:00
VariantDef::Struct(s) => {
2019-06-10 23:34:34 +00:00
ss = s.source(db);
(ss.file_id, ss.ast.kind())
2019-01-25 17:32:34 +00:00
}
VariantDef::EnumVariant(e) => {
2019-06-11 00:17:29 +00:00
es = e.source(db);
(es.file_id, es.ast.kind())
2019-01-25 17:32:34 +00:00
}
};
2019-04-02 09:48:14 +00:00
let field_sources = match struct_kind {
2019-09-25 01:32:01 +00:00
ast::StructKind::Tuple(fl) => fl.fields().map(|it| FieldSource::Pos(it)).collect(),
ast::StructKind::Named(fl) => fl.fields().map(|it| FieldSource::Named(it)).collect(),
2019-04-02 09:48:14 +00:00
ast::StructKind::Unit => Vec::new(),
2019-01-25 17:32:34 +00:00
};
2019-06-11 15:07:42 +00:00
let ast = field_sources
2019-01-25 17:32:34 +00:00
.into_iter()
.zip(fields.iter())
.find(|(_syntax, (id, _))| *id == self.id)
.unwrap()
.0;
2019-06-11 15:07:42 +00:00
Source { file_id, ast }
2019-01-25 17:32:34 +00:00
}
}