2019-10-31 13:40:36 +00:00
|
|
|
//! Defines hir-level representation of structs, enums and unions
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2019-12-03 16:07:56 +00:00
|
|
|
use either::Either;
|
2019-11-22 18:43:36 +00:00
|
|
|
use hir_expand::{
|
|
|
|
name::{AsName, Name},
|
2019-11-28 09:50:26 +00:00
|
|
|
InFile,
|
2019-11-22 18:43:36 +00:00
|
|
|
};
|
|
|
|
use ra_arena::{map::ArenaMap, Arena};
|
2019-12-21 18:40:20 +00:00
|
|
|
use ra_prof::profile;
|
2019-12-26 15:22:15 +00:00
|
|
|
use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner, VisibilityOwner};
|
2019-10-31 13:40:36 +00:00
|
|
|
|
|
|
|
use crate::{
|
2020-04-30 10:20:13 +00:00
|
|
|
body::{CfgExpander, LowerCtx},
|
|
|
|
db::DefDatabase,
|
|
|
|
src::HasChildSource,
|
|
|
|
src::HasSource,
|
|
|
|
trace::Trace,
|
|
|
|
type_ref::TypeRef,
|
|
|
|
visibility::RawVisibility,
|
|
|
|
EnumId, HasModule, LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StructId, UnionId,
|
|
|
|
VariantId,
|
2019-10-31 13:40:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Note that we use `StructData` for unions as well!
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct StructData {
|
2019-11-27 20:22:20 +00:00
|
|
|
pub name: Name,
|
2019-10-31 13:40:36 +00:00
|
|
|
pub variant_data: Arc<VariantData>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct EnumData {
|
2019-11-27 20:22:20 +00:00
|
|
|
pub name: Name,
|
2020-03-19 15:00:11 +00:00
|
|
|
pub variants: Arena<EnumVariantData>,
|
2019-10-31 13:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct EnumVariantData {
|
2019-11-27 20:22:20 +00:00
|
|
|
pub name: Name,
|
2019-10-31 13:40:36 +00:00
|
|
|
pub variant_data: Arc<VariantData>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2019-11-22 18:52:06 +00:00
|
|
|
pub enum VariantData {
|
2020-04-25 12:23:34 +00:00
|
|
|
Record(Arena<FieldData>),
|
|
|
|
Tuple(Arena<FieldData>),
|
2019-10-31 13:40:36 +00:00
|
|
|
Unit,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A single field of an enum variant or struct
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2020-04-25 12:23:34 +00:00
|
|
|
pub struct FieldData {
|
2019-10-31 13:40:36 +00:00
|
|
|
pub name: Name,
|
|
|
|
pub type_ref: TypeRef,
|
2019-12-26 15:22:15 +00:00
|
|
|
pub visibility: RawVisibility,
|
2019-10-31 13:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl StructData {
|
2020-03-13 15:05:46 +00:00
|
|
|
pub(crate) fn struct_data_query(db: &dyn DefDatabase, id: StructId) -> Arc<StructData> {
|
2019-12-12 13:58:04 +00:00
|
|
|
let src = id.lookup(db).source(db);
|
2020-04-07 15:58:05 +00:00
|
|
|
|
2019-11-27 20:22:20 +00:00
|
|
|
let name = src.value.name().map_or_else(Name::missing, |n| n.as_name());
|
2020-04-09 16:32:02 +00:00
|
|
|
let variant_data =
|
|
|
|
VariantData::new(db, src.map(|s| s.kind()), id.lookup(db).container.module(db));
|
2019-10-31 15:45:10 +00:00
|
|
|
let variant_data = Arc::new(variant_data);
|
|
|
|
Arc::new(StructData { name, variant_data })
|
|
|
|
}
|
2020-03-13 15:05:46 +00:00
|
|
|
pub(crate) fn union_data_query(db: &dyn DefDatabase, id: UnionId) -> Arc<StructData> {
|
2019-12-12 14:11:57 +00:00
|
|
|
let src = id.lookup(db).source(db);
|
2019-11-27 20:22:20 +00:00
|
|
|
let name = src.value.name().map_or_else(Name::missing, |n| n.as_name());
|
2019-11-25 14:30:50 +00:00
|
|
|
let variant_data = VariantData::new(
|
2019-12-26 15:22:15 +00:00
|
|
|
db,
|
|
|
|
src.map(|s| {
|
|
|
|
s.record_field_def_list()
|
|
|
|
.map(ast::StructKind::Record)
|
|
|
|
.unwrap_or(ast::StructKind::Unit)
|
|
|
|
}),
|
2020-04-09 16:32:02 +00:00
|
|
|
id.lookup(db).container.module(db),
|
2019-11-25 14:30:50 +00:00
|
|
|
);
|
|
|
|
let variant_data = Arc::new(variant_data);
|
|
|
|
Arc::new(StructData { name, variant_data })
|
|
|
|
}
|
2019-10-31 13:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EnumData {
|
2020-03-13 15:05:46 +00:00
|
|
|
pub(crate) fn enum_data_query(db: &dyn DefDatabase, e: EnumId) -> Arc<EnumData> {
|
2019-12-21 18:40:20 +00:00
|
|
|
let _p = profile("enum_data_query");
|
2019-12-12 14:11:57 +00:00
|
|
|
let src = e.lookup(db).source(db);
|
2019-11-27 20:22:20 +00:00
|
|
|
let name = src.value.name().map_or_else(Name::missing, |n| n.as_name());
|
2019-11-22 18:43:36 +00:00
|
|
|
let mut trace = Trace::new_for_arena();
|
2020-04-09 16:32:02 +00:00
|
|
|
lower_enum(db, &mut trace, &src, e.lookup(db).container.module(db));
|
2019-11-22 18:43:36 +00:00
|
|
|
Arc::new(EnumData { name, variants: trace.into_arena() })
|
2019-10-31 13:40:36 +00:00
|
|
|
}
|
2019-10-31 15:45:10 +00:00
|
|
|
|
2019-11-27 20:22:20 +00:00
|
|
|
pub fn variant(&self, name: &Name) -> Option<LocalEnumVariantId> {
|
|
|
|
let (id, _) = self.variants.iter().find(|(_id, data)| &data.name == name)?;
|
2019-10-31 15:45:10 +00:00
|
|
|
Some(id)
|
|
|
|
}
|
2019-10-31 13:40:36 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 18:43:36 +00:00
|
|
|
impl HasChildSource for EnumId {
|
|
|
|
type ChildId = LocalEnumVariantId;
|
|
|
|
type Value = ast::EnumVariant;
|
2020-03-13 15:05:46 +00:00
|
|
|
fn child_source(&self, db: &dyn DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>> {
|
2019-12-12 14:11:57 +00:00
|
|
|
let src = self.lookup(db).source(db);
|
2019-11-22 18:43:36 +00:00
|
|
|
let mut trace = Trace::new_for_map();
|
2020-04-09 16:32:02 +00:00
|
|
|
lower_enum(db, &mut trace, &src, self.lookup(db).container.module(db));
|
2019-11-22 18:43:36 +00:00
|
|
|
src.with_value(trace.into_map())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lower_enum(
|
2020-03-13 15:05:46 +00:00
|
|
|
db: &dyn DefDatabase,
|
2020-03-19 15:00:11 +00:00
|
|
|
trace: &mut Trace<EnumVariantData, ast::EnumVariant>,
|
2019-12-26 15:22:15 +00:00
|
|
|
ast: &InFile<ast::EnumDef>,
|
2020-04-09 16:32:02 +00:00
|
|
|
module_id: ModuleId,
|
2019-11-22 18:43:36 +00:00
|
|
|
) {
|
2020-05-03 15:56:45 +00:00
|
|
|
let expander = CfgExpander::new(db, ast.file_id, module_id.krate);
|
|
|
|
let variants =
|
|
|
|
ast.value.variant_list().into_iter().flat_map(|it| it.variants()).filter(|var| {
|
|
|
|
let attrs = expander.parse_attrs(var);
|
|
|
|
expander.is_cfg_enabled(&attrs)
|
|
|
|
});
|
|
|
|
for var in variants {
|
2019-11-22 18:43:36 +00:00
|
|
|
trace.alloc(
|
|
|
|
|| var.clone(),
|
|
|
|
|| EnumVariantData {
|
2019-11-27 20:22:20 +00:00
|
|
|
name: var.name().map_or_else(Name::missing, |it| it.as_name()),
|
2020-04-09 16:32:02 +00:00
|
|
|
variant_data: Arc::new(VariantData::new(db, ast.with_value(var.kind()), module_id)),
|
2019-11-22 18:43:36 +00:00
|
|
|
},
|
2019-11-24 14:49:49 +00:00
|
|
|
);
|
2019-11-22 18:43:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 13:40:36 +00:00
|
|
|
impl VariantData {
|
2020-04-09 16:32:02 +00:00
|
|
|
fn new(db: &dyn DefDatabase, flavor: InFile<ast::StructKind>, module_id: ModuleId) -> Self {
|
2020-04-11 15:52:26 +00:00
|
|
|
let mut expander = CfgExpander::new(db, flavor.file_id, module_id.krate);
|
2019-11-22 18:43:36 +00:00
|
|
|
let mut trace = Trace::new_for_arena();
|
2020-04-11 15:17:12 +00:00
|
|
|
match lower_struct(db, &mut expander, &mut trace, &flavor) {
|
2019-11-22 18:43:36 +00:00
|
|
|
StructKind::Tuple => VariantData::Tuple(trace.into_arena()),
|
|
|
|
StructKind::Record => VariantData::Record(trace.into_arena()),
|
|
|
|
StructKind::Unit => VariantData::Unit,
|
2019-11-22 18:52:06 +00:00
|
|
|
}
|
2019-10-31 13:40:36 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 12:23:34 +00:00
|
|
|
pub fn fields(&self) -> &Arena<FieldData> {
|
|
|
|
const EMPTY: &Arena<FieldData> = &Arena::new();
|
2019-11-22 18:43:36 +00:00
|
|
|
match &self {
|
2019-11-24 19:44:24 +00:00
|
|
|
VariantData::Record(fields) | VariantData::Tuple(fields) => fields,
|
|
|
|
_ => EMPTY,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 12:23:34 +00:00
|
|
|
pub fn field(&self, name: &Name) -> Option<LocalFieldId> {
|
2019-11-26 11:29:12 +00:00
|
|
|
self.fields().iter().find_map(|(id, data)| if &data.name == name { Some(id) } else { None })
|
|
|
|
}
|
|
|
|
|
2020-02-15 20:48:20 +00:00
|
|
|
pub fn kind(&self) -> StructKind {
|
|
|
|
match self {
|
|
|
|
VariantData::Record(_) => StructKind::Record,
|
|
|
|
VariantData::Tuple(_) => StructKind::Tuple,
|
|
|
|
VariantData::Unit => StructKind::Unit,
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 13:40:36 +00:00
|
|
|
}
|
2019-11-22 18:43:36 +00:00
|
|
|
|
|
|
|
impl HasChildSource for VariantId {
|
2020-04-25 12:23:34 +00:00
|
|
|
type ChildId = LocalFieldId;
|
2019-11-22 18:43:36 +00:00
|
|
|
type Value = Either<ast::TupleFieldDef, ast::RecordFieldDef>;
|
|
|
|
|
2020-03-13 15:05:46 +00:00
|
|
|
fn child_source(&self, db: &dyn DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>> {
|
2020-04-09 16:32:02 +00:00
|
|
|
let (src, module_id) = match self {
|
2019-11-22 18:43:36 +00:00
|
|
|
VariantId::EnumVariantId(it) => {
|
|
|
|
// I don't really like the fact that we call into parent source
|
|
|
|
// here, this might add to more queries then necessary.
|
|
|
|
let src = it.parent.child_source(db);
|
2020-04-09 16:32:02 +00:00
|
|
|
(src.map(|map| map[it.local_id].kind()), it.parent.lookup(db).container.module(db))
|
2019-11-22 18:43:36 +00:00
|
|
|
}
|
2020-04-09 16:32:02 +00:00
|
|
|
VariantId::StructId(it) => {
|
|
|
|
(it.lookup(db).source(db).map(|it| it.kind()), it.lookup(db).container.module(db))
|
|
|
|
}
|
|
|
|
VariantId::UnionId(it) => (
|
|
|
|
it.lookup(db).source(db).map(|it| {
|
|
|
|
it.record_field_def_list()
|
|
|
|
.map(ast::StructKind::Record)
|
|
|
|
.unwrap_or(ast::StructKind::Unit)
|
|
|
|
}),
|
|
|
|
it.lookup(db).container.module(db),
|
|
|
|
),
|
2019-11-22 18:43:36 +00:00
|
|
|
};
|
2020-04-11 15:52:26 +00:00
|
|
|
let mut expander = CfgExpander::new(db, src.file_id, module_id.krate);
|
2019-11-22 18:43:36 +00:00
|
|
|
let mut trace = Trace::new_for_map();
|
2020-04-11 15:17:12 +00:00
|
|
|
lower_struct(db, &mut expander, &mut trace, &src);
|
2019-11-22 18:43:36 +00:00
|
|
|
src.with_value(trace.into_map())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 15:56:34 +00:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
2020-02-15 20:48:20 +00:00
|
|
|
pub enum StructKind {
|
2019-11-22 18:43:36 +00:00
|
|
|
Tuple,
|
|
|
|
Record,
|
|
|
|
Unit,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lower_struct(
|
2020-03-13 15:05:46 +00:00
|
|
|
db: &dyn DefDatabase,
|
2020-04-11 15:52:26 +00:00
|
|
|
expander: &mut CfgExpander,
|
2020-04-25 12:23:34 +00:00
|
|
|
trace: &mut Trace<FieldData, Either<ast::TupleFieldDef, ast::RecordFieldDef>>,
|
2019-12-26 15:22:15 +00:00
|
|
|
ast: &InFile<ast::StructKind>,
|
2019-11-22 18:43:36 +00:00
|
|
|
) -> StructKind {
|
2020-04-30 10:20:13 +00:00
|
|
|
let ctx = LowerCtx::new(db, ast.file_id);
|
|
|
|
|
2019-12-26 15:22:15 +00:00
|
|
|
match &ast.value {
|
2019-11-22 18:43:36 +00:00
|
|
|
ast::StructKind::Tuple(fl) => {
|
|
|
|
for (i, fd) in fl.fields().enumerate() {
|
2020-04-11 15:17:12 +00:00
|
|
|
let attrs = expander.parse_attrs(&fd);
|
2020-04-11 15:20:26 +00:00
|
|
|
if !expander.is_cfg_enabled(&attrs) {
|
2020-04-09 16:32:02 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-22 18:43:36 +00:00
|
|
|
trace.alloc(
|
2019-12-03 16:07:56 +00:00
|
|
|
|| Either::Left(fd.clone()),
|
2020-04-25 12:23:34 +00:00
|
|
|
|| FieldData {
|
2019-11-22 18:43:36 +00:00
|
|
|
name: Name::new_tuple_field(i),
|
2020-04-30 10:20:13 +00:00
|
|
|
type_ref: TypeRef::from_ast_opt(&ctx, fd.type_ref()),
|
2019-12-26 15:22:15 +00:00
|
|
|
visibility: RawVisibility::from_ast(db, ast.with_value(fd.visibility())),
|
2019-11-22 18:43:36 +00:00
|
|
|
},
|
2019-11-24 14:49:49 +00:00
|
|
|
);
|
2019-11-22 18:43:36 +00:00
|
|
|
}
|
|
|
|
StructKind::Tuple
|
|
|
|
}
|
|
|
|
ast::StructKind::Record(fl) => {
|
|
|
|
for fd in fl.fields() {
|
2020-04-11 15:17:12 +00:00
|
|
|
let attrs = expander.parse_attrs(&fd);
|
2020-04-11 15:20:26 +00:00
|
|
|
if !expander.is_cfg_enabled(&attrs) {
|
2020-04-09 16:32:02 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-22 18:43:36 +00:00
|
|
|
trace.alloc(
|
2019-12-03 16:07:56 +00:00
|
|
|
|| Either::Right(fd.clone()),
|
2020-04-25 12:23:34 +00:00
|
|
|
|| FieldData {
|
2019-11-22 18:43:36 +00:00
|
|
|
name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing),
|
2020-04-30 10:20:13 +00:00
|
|
|
type_ref: TypeRef::from_ast_opt(&ctx, fd.ascribed_type()),
|
2019-12-26 15:22:15 +00:00
|
|
|
visibility: RawVisibility::from_ast(db, ast.with_value(fd.visibility())),
|
2019-11-22 18:43:36 +00:00
|
|
|
},
|
2019-11-24 14:49:49 +00:00
|
|
|
);
|
2019-11-22 18:43:36 +00:00
|
|
|
}
|
|
|
|
StructKind::Record
|
|
|
|
}
|
|
|
|
ast::StructKind::Unit => StructKind::Unit,
|
|
|
|
}
|
|
|
|
}
|