rust/crates/ra_hir/src/adt.rs

229 lines
6.7 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_syntax::{
SyntaxNode,
ast::{self, NameOwner, StructFlavor, AstNode}
};
use crate::{
DefId, DefLoc, Name, AsName, Struct, Enum, EnumVariant,
HirDatabase, DefKind,
SourceItemId,
type_ref::TypeRef,
};
impl Struct {
pub(crate) fn new(def_id: DefId) -> Self {
Struct { def_id }
}
2019-01-09 16:46:02 +01:00
2019-01-15 18:43:37 +01:00
pub(crate) fn variant_data(&self, db: &impl HirDatabase) -> Arc<VariantData> {
db.struct_data(self.def_id).variant_data.clone()
2019-01-09 16:46:02 +01:00
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StructData {
2019-01-08 13:19:37 +01:00
pub(crate) name: Option<Name>,
pub(crate) variant_data: Arc<VariantData>,
}
impl StructData {
2019-01-08 13:38:29 +01:00
fn new(struct_def: &ast::StructDef) -> StructData {
2018-12-28 19:34:58 +01:00
let name = struct_def.name().map(|n| n.as_name());
let variant_data = VariantData::new(struct_def.flavor());
let variant_data = Arc::new(variant_data);
StructData { name, variant_data }
}
2019-01-08 13:38:29 +01:00
2019-01-15 16:43:25 +01:00
pub(crate) fn struct_data_query(db: &impl HirDatabase, def_id: DefId) -> Arc<StructData> {
2019-01-08 13:38:29 +01:00
let def_loc = def_id.loc(db);
assert!(def_loc.kind == DefKind::Struct);
let syntax = db.file_item(def_loc.source_item_id);
let struct_def =
ast::StructDef::cast(&syntax).expect("struct def should point to StructDef node");
2019-01-15 16:43:25 +01:00
Arc::new(StructData::new(struct_def))
2019-01-08 13:38:29 +01:00
}
}
fn get_def_id(
db: &impl HirDatabase,
same_file_loc: &DefLoc,
node: &SyntaxNode,
expected_kind: DefKind,
) -> DefId {
let file_id = same_file_loc.source_item_id.file_id;
let file_items = db.file_items(file_id);
let item_id = file_items.id_of(file_id, node);
let source_item_id = SourceItemId {
item_id: Some(item_id),
..same_file_loc.source_item_id
};
let loc = DefLoc {
module: same_file_loc.module,
kind: expected_kind,
source_item_id,
};
loc.id(db)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EnumData {
2019-01-08 13:22:57 +01:00
pub(crate) name: Option<Name>,
pub(crate) variants: Vec<(Name, EnumVariant)>,
}
impl EnumData {
fn new(enum_def: &ast::EnumDef, variants: Vec<(Name, EnumVariant)>) -> Self {
2018-12-28 19:34:58 +01:00
let name = enum_def.name().map(|n| n.as_name());
EnumData { name, variants }
}
2019-01-08 13:38:29 +01:00
2019-01-15 16:43:25 +01:00
pub(crate) fn enum_data_query(db: &impl HirDatabase, def_id: DefId) -> Arc<EnumData> {
2019-01-08 13:38:29 +01:00
let def_loc = def_id.loc(db);
assert!(def_loc.kind == DefKind::Enum);
let syntax = db.file_item(def_loc.source_item_id);
let enum_def = ast::EnumDef::cast(&syntax).expect("enum def should point to EnumDef node");
let variants = if let Some(vl) = enum_def.variant_list() {
vl.variants()
.filter_map(|variant_def| {
let name = variant_def.name().map(|n| n.as_name());
name.map(|n| {
let def_id =
get_def_id(db, &def_loc, variant_def.syntax(), DefKind::EnumVariant);
(n, EnumVariant::new(def_id))
})
})
.collect()
} else {
Vec::new()
};
2019-01-15 16:43:25 +01:00
Arc::new(EnumData::new(enum_def, variants))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EnumVariantData {
pub(crate) name: Option<Name>,
pub(crate) variant_data: Arc<VariantData>,
pub(crate) parent_enum: Enum,
}
impl EnumVariantData {
fn new(variant_def: &ast::EnumVariant, parent_enum: Enum) -> EnumVariantData {
let name = variant_def.name().map(|n| n.as_name());
let variant_data = VariantData::new(variant_def.flavor());
let variant_data = Arc::new(variant_data);
EnumVariantData {
name,
variant_data,
parent_enum,
}
}
pub(crate) fn enum_variant_data_query(
db: &impl HirDatabase,
def_id: DefId,
2019-01-15 16:43:25 +01:00
) -> Arc<EnumVariantData> {
let def_loc = def_id.loc(db);
assert!(def_loc.kind == DefKind::EnumVariant);
let syntax = db.file_item(def_loc.source_item_id);
let variant_def = ast::EnumVariant::cast(&syntax)
.expect("enum variant def should point to EnumVariant node");
let enum_node = syntax
.parent()
.expect("enum variant should have enum variant list ancestor")
.parent()
.expect("enum variant list should have enum ancestor");
let enum_def_id = get_def_id(db, &def_loc, enum_node, DefKind::Enum);
2019-01-15 16:43:25 +01:00
Arc::new(EnumVariantData::new(variant_def, Enum::new(enum_def_id)))
2019-01-08 13:38:29 +01:00
}
}
2019-01-09 16:46:02 +01:00
/// A single field of an enum variant or struct
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StructField {
pub(crate) name: Name,
pub(crate) type_ref: TypeRef,
}
/// Fields of an enum variant or struct
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VariantData {
Struct(Vec<StructField>),
Tuple(Vec<StructField>),
Unit,
}
impl VariantData {
pub fn fields(&self) -> &[StructField] {
match self {
VariantData::Struct(fields) | VariantData::Tuple(fields) => fields,
_ => &[],
}
}
pub fn is_struct(&self) -> bool {
match self {
VariantData::Struct(..) => true,
_ => false,
}
}
pub fn is_tuple(&self) -> bool {
match self {
VariantData::Tuple(..) => true,
_ => false,
}
}
pub fn is_unit(&self) -> bool {
match self {
VariantData::Unit => true,
_ => false,
}
}
}
impl VariantData {
2019-01-08 13:40:02 +01:00
fn new(flavor: StructFlavor) -> Self {
match flavor {
StructFlavor::Tuple(fl) => {
let fields = fl
.fields()
.enumerate()
.map(|(i, fd)| StructField {
2018-12-28 19:34:58 +01:00
name: Name::tuple_field_name(i),
type_ref: TypeRef::from_ast_opt(fd.type_ref()),
})
.collect();
VariantData::Tuple(fields)
}
StructFlavor::Named(fl) => {
let fields = fl
.fields()
.map(|fd| StructField {
2018-12-28 19:34:58 +01:00
name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing),
type_ref: TypeRef::from_ast_opt(fd.type_ref()),
})
.collect();
VariantData::Struct(fields)
}
StructFlavor::Unit => VariantData::Unit,
}
}
2018-12-25 13:54:38 +01:00
2018-12-28 19:34:58 +01:00
pub(crate) fn get_field_type_ref(&self, field_name: &Name) -> Option<&TypeRef> {
2018-12-25 15:15:40 +01:00
self.fields()
.iter()
2019-01-09 16:46:02 +01:00
.find(|f| f.name == *field_name)
.map(|f| &f.type_ref)
2018-12-25 13:54:38 +01:00
}
}