2019-01-12 14:27:35 -06:00
|
|
|
//! This module contains the implementation details of the HIR for ADTs, i.e.
|
|
|
|
//! structs and enums (and unions).
|
|
|
|
|
2018-12-24 14:00:14 -06:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2019-01-25 03:41:23 -06:00
|
|
|
use ra_arena::{RawId, Arena, impl_arena_id};
|
2019-01-25 02:35:38 -06:00
|
|
|
use ra_syntax::{
|
|
|
|
TreeArc,
|
|
|
|
ast::{self, NameOwner, StructFlavor}
|
|
|
|
};
|
2018-12-24 12:07:48 -06:00
|
|
|
|
|
|
|
use crate::{
|
2019-01-24 16:53:07 -06:00
|
|
|
Name, AsName, Struct, Enum, EnumVariant, Crate,
|
2019-01-25 11:32:34 -06:00
|
|
|
HirDatabase, HirFileId, StructField, FieldSource,
|
2018-12-25 14:40:33 -06:00
|
|
|
type_ref::TypeRef,
|
2018-12-24 12:07:48 -06:00
|
|
|
};
|
|
|
|
|
2019-01-24 16:53:07 -06:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
|
|
|
pub enum AdtDef {
|
|
|
|
Struct(Struct),
|
|
|
|
Enum(Enum),
|
|
|
|
}
|
|
|
|
impl_froms!(AdtDef: Struct, Enum);
|
|
|
|
|
|
|
|
impl AdtDef {
|
|
|
|
pub(crate) fn krate(self, db: &impl HirDatabase) -> Option<Crate> {
|
|
|
|
match self {
|
|
|
|
AdtDef::Struct(s) => s.module(db),
|
|
|
|
AdtDef::Enum(e) => e.module(db),
|
|
|
|
}
|
|
|
|
.krate(db)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-24 12:07:48 -06:00
|
|
|
impl Struct {
|
2019-01-15 11:43:37 -06:00
|
|
|
pub(crate) fn variant_data(&self, db: &impl HirDatabase) -> Arc<VariantData> {
|
2019-01-24 08:54:18 -06:00
|
|
|
db.struct_data((*self).into()).variant_data.clone()
|
2019-01-09 09:46:02 -06:00
|
|
|
}
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct StructData {
|
2019-01-08 06:19:37 -06:00
|
|
|
pub(crate) name: Option<Name>,
|
|
|
|
pub(crate) variant_data: Arc<VariantData>,
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl StructData {
|
2019-01-08 06:38:29 -06:00
|
|
|
fn new(struct_def: &ast::StructDef) -> StructData {
|
2018-12-28 12:34:58 -06:00
|
|
|
let name = struct_def.name().map(|n| n.as_name());
|
2018-12-25 14:40:33 -06:00
|
|
|
let variant_data = VariantData::new(struct_def.flavor());
|
2018-12-24 14:00:14 -06:00
|
|
|
let variant_data = Arc::new(variant_data);
|
2018-12-25 14:40:33 -06:00
|
|
|
StructData { name, variant_data }
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|
2019-01-08 06:38:29 -06:00
|
|
|
|
2019-01-24 08:54:18 -06:00
|
|
|
pub(crate) fn struct_data_query(db: &impl HirDatabase, struct_: Struct) -> Arc<StructData> {
|
|
|
|
let (_, struct_def) = struct_.source(db);
|
|
|
|
Arc::new(StructData::new(&*struct_def))
|
2019-01-08 06:38:29 -06:00
|
|
|
}
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|
|
|
|
|
2019-01-25 02:35:38 -06:00
|
|
|
fn variants(enum_def: &ast::EnumDef) -> impl Iterator<Item = &ast::EnumVariant> {
|
|
|
|
enum_def
|
|
|
|
.variant_list()
|
|
|
|
.into_iter()
|
|
|
|
.flat_map(|it| it.variants())
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EnumVariant {
|
2019-01-25 03:41:23 -06:00
|
|
|
pub(crate) fn source_impl(
|
|
|
|
&self,
|
|
|
|
db: &impl HirDatabase,
|
|
|
|
) -> (HirFileId, TreeArc<ast::EnumVariant>) {
|
2019-01-25 02:35:38 -06:00
|
|
|
let (file_id, enum_def) = self.parent.source(db);
|
|
|
|
let var = variants(&*enum_def)
|
2019-01-25 03:41:23 -06:00
|
|
|
.zip(db.enum_data(self.parent).variants.iter())
|
|
|
|
.find(|(_syntax, (id, _))| *id == self.id)
|
2019-01-25 02:35:38 -06:00
|
|
|
.unwrap()
|
2019-01-25 03:41:23 -06:00
|
|
|
.0
|
2019-01-25 02:35:38 -06:00
|
|
|
.to_owned();
|
|
|
|
(file_id, var)
|
|
|
|
}
|
2019-01-25 05:21:14 -06:00
|
|
|
pub(crate) fn variant_data(&self, db: &impl HirDatabase) -> Arc<VariantData> {
|
|
|
|
db.enum_data(self.parent).variants[self.id]
|
|
|
|
.variant_data
|
|
|
|
.clone()
|
|
|
|
}
|
2019-01-25 02:35:38 -06:00
|
|
|
}
|
|
|
|
|
2018-12-24 12:07:48 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct EnumData {
|
2019-01-08 06:22:57 -06:00
|
|
|
pub(crate) name: Option<Name>,
|
2019-01-25 03:41:23 -06:00
|
|
|
pub(crate) variants: Arena<EnumVariantId, EnumVariantData>,
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EnumData {
|
2019-01-24 09:56:38 -06:00
|
|
|
pub(crate) fn enum_data_query(db: &impl HirDatabase, e: Enum) -> Arc<EnumData> {
|
2019-01-25 02:35:38 -06:00
|
|
|
let (_file_id, enum_def) = e.source(db);
|
2019-01-25 05:21:14 -06:00
|
|
|
let name = enum_def.name().map(|n| n.as_name());
|
|
|
|
let variants = variants(&*enum_def)
|
|
|
|
.map(|var| EnumVariantData {
|
2019-01-25 03:41:23 -06:00
|
|
|
name: var.name().map(|it| it.as_name()),
|
|
|
|
variant_data: Arc::new(VariantData::new(var.flavor())),
|
2019-01-25 05:21:14 -06:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
Arc::new(EnumData { name, variants })
|
2019-01-08 09:01:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 05:21:14 -06:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub(crate) struct EnumVariantId(RawId);
|
|
|
|
impl_arena_id!(EnumVariantId);
|
|
|
|
|
2019-01-08 09:01:19 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2019-01-25 05:25:30 -06:00
|
|
|
pub(crate) struct EnumVariantData {
|
2019-01-08 09:01:19 -06:00
|
|
|
pub(crate) name: Option<Name>,
|
2019-01-25 05:25:30 -06:00
|
|
|
variant_data: Arc<VariantData>,
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|
|
|
|
|
2019-01-25 05:21:14 -06:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub(crate) struct StructFieldId(RawId);
|
|
|
|
impl_arena_id!(StructFieldId);
|
|
|
|
|
2019-01-09 09:46:02 -06:00
|
|
|
/// A single field of an enum variant or struct
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2019-01-25 03:46:23 -06:00
|
|
|
pub struct StructFieldData {
|
2019-01-09 09:46:02 -06: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 05:25:30 -06:00
|
|
|
pub(crate) struct VariantData(VariantDataInner);
|
2019-01-25 05:21:14 -06:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
enum VariantDataInner {
|
|
|
|
Struct(Arena<StructFieldId, StructFieldData>),
|
|
|
|
Tuple(Arena<StructFieldId, StructFieldData>),
|
2019-01-09 09:46:02 -06:00
|
|
|
Unit,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VariantData {
|
2019-01-25 05:21:14 -06: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 09:46:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-24 12:07:48 -06:00
|
|
|
impl VariantData {
|
2019-01-08 06:40:02 -06:00
|
|
|
fn new(flavor: StructFlavor) -> Self {
|
2019-01-25 05:21:14 -06:00
|
|
|
let inner = match flavor {
|
2019-01-25 11:32:34 -06:00
|
|
|
ast::StructFlavor::Tuple(fl) => {
|
2018-12-25 06:31:30 -06:00
|
|
|
let fields = fl
|
|
|
|
.fields()
|
|
|
|
.enumerate()
|
2019-01-25 03:46:23 -06:00
|
|
|
.map(|(i, fd)| StructFieldData {
|
2018-12-28 12:34:58 -06:00
|
|
|
name: Name::tuple_field_name(i),
|
2018-12-25 14:40:33 -06:00
|
|
|
type_ref: TypeRef::from_ast_opt(fd.type_ref()),
|
2018-12-25 06:31:30 -06:00
|
|
|
})
|
2018-12-25 14:40:33 -06:00
|
|
|
.collect();
|
2019-01-25 05:21:14 -06:00
|
|
|
VariantDataInner::Tuple(fields)
|
2018-12-25 06:31:30 -06:00
|
|
|
}
|
2019-01-25 11:32:34 -06:00
|
|
|
ast::StructFlavor::Named(fl) => {
|
2018-12-25 06:31:30 -06:00
|
|
|
let fields = fl
|
|
|
|
.fields()
|
2019-01-25 03:46:23 -06:00
|
|
|
.map(|fd| StructFieldData {
|
2018-12-28 12:34:58 -06:00
|
|
|
name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing),
|
2018-12-25 14:40:33 -06:00
|
|
|
type_ref: TypeRef::from_ast_opt(fd.type_ref()),
|
2018-12-25 06:31:30 -06:00
|
|
|
})
|
2018-12-25 14:40:33 -06:00
|
|
|
.collect();
|
2019-01-25 05:21:14 -06:00
|
|
|
VariantDataInner::Struct(fields)
|
2018-12-25 06:31:30 -06:00
|
|
|
}
|
2019-01-25 11:32:34 -06:00
|
|
|
ast::StructFlavor::Unit => VariantDataInner::Unit,
|
2019-01-25 05:21:14 -06:00
|
|
|
};
|
|
|
|
VariantData(inner)
|
2018-12-25 06:31:30 -06:00
|
|
|
}
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|
2019-01-25 11:32:34 -06:00
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
|
|
|
pub enum VariantDef {
|
|
|
|
Struct(Struct),
|
|
|
|
EnumVariant(EnumVariant),
|
|
|
|
}
|
|
|
|
impl_froms!(VariantDef: Struct, EnumVariant);
|
|
|
|
|
|
|
|
impl VariantDef {
|
|
|
|
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 HirDatabase) -> Arc<VariantData> {
|
|
|
|
match self {
|
|
|
|
VariantDef::Struct(it) => it.variant_data(db),
|
|
|
|
VariantDef::EnumVariant(it) => it.variant_data(db),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StructField {
|
|
|
|
pub(crate) fn source_impl(&self, db: &impl HirDatabase) -> (HirFileId, FieldSource) {
|
|
|
|
let var_data = self.parent.variant_data(db);
|
|
|
|
let fields = var_data.fields().unwrap();
|
|
|
|
let ss;
|
|
|
|
let es;
|
|
|
|
let (file_id, struct_flavor) = match self.parent {
|
|
|
|
VariantDef::Struct(s) => {
|
|
|
|
let (file_id, source) = s.source(db);
|
|
|
|
ss = source;
|
|
|
|
(file_id, ss.flavor())
|
|
|
|
}
|
|
|
|
VariantDef::EnumVariant(e) => {
|
|
|
|
let (file_id, source) = e.source(db);
|
|
|
|
es = source;
|
|
|
|
(file_id, es.flavor())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let field_sources = match struct_flavor {
|
|
|
|
ast::StructFlavor::Tuple(fl) => fl
|
|
|
|
.fields()
|
|
|
|
.map(|it| FieldSource::Pos(it.to_owned()))
|
|
|
|
.collect(),
|
|
|
|
ast::StructFlavor::Named(fl) => fl
|
|
|
|
.fields()
|
|
|
|
.map(|it| FieldSource::Named(it.to_owned()))
|
|
|
|
.collect(),
|
|
|
|
ast::StructFlavor::Unit => Vec::new(),
|
|
|
|
};
|
|
|
|
let field = field_sources
|
|
|
|
.into_iter()
|
|
|
|
.zip(fields.iter())
|
|
|
|
.find(|(_syntax, (id, _))| *id == self.id)
|
|
|
|
.unwrap()
|
|
|
|
.0;
|
|
|
|
(file_id, field)
|
|
|
|
}
|
|
|
|
}
|