rust/crates/ra_hir/src/adt.rs

176 lines
4.5 KiB
Rust
Raw Normal View History

use std::sync::Arc;
2019-01-01 22:30:00 +01:00
use ra_db::Cancelable;
2018-12-28 19:34:58 +01:00
use ra_syntax::ast::{self, NameOwner, StructFlavor};
use crate::{
2019-01-08 13:19:37 +01:00
DefId, Name, AsName, Struct,
2018-12-28 19:34:58 +01:00
db::HirDatabase,
type_ref::TypeRef,
};
impl Struct {
pub(crate) fn new(def_id: DefId) -> Self {
Struct { def_id }
}
2019-01-08 13:19:37 +01:00
pub(crate) fn struct_data(&self, db: &impl HirDatabase) -> Cancelable<Arc<StructData>> {
Ok(db.struct_data(self.def_id)?)
}
}
#[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 09:28:42 +01:00
pub(crate) 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 }
}
2018-12-28 19:34:58 +01:00
pub fn name(&self) -> Option<&Name> {
self.name.as_ref()
}
pub fn variant_data(&self) -> &Arc<VariantData> {
&self.variant_data
}
}
pub struct Enum {
def_id: DefId,
}
impl Enum {
pub(crate) fn new(def_id: DefId) -> Self {
Enum { def_id }
}
pub fn def_id(&self) -> DefId {
self.def_id
}
2018-12-28 19:34:58 +01:00
pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> {
Ok(db.enum_data(self.def_id)?.name.clone())
}
2018-12-28 19:06:08 +01:00
2018-12-28 19:34:58 +01:00
pub fn variants(&self, db: &impl HirDatabase) -> Cancelable<Vec<(Name, Arc<VariantData>)>> {
2018-12-28 19:06:08 +01:00
Ok(db.enum_data(self.def_id)?.variants.clone())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EnumData {
2018-12-28 19:34:58 +01:00
name: Option<Name>,
variants: Vec<(Name, Arc<VariantData>)>,
}
impl EnumData {
2019-01-08 09:28:42 +01:00
pub(crate) fn new(enum_def: &ast::EnumDef) -> Self {
2018-12-28 19:34:58 +01:00
let name = enum_def.name().map(|n| n.as_name());
let variants = if let Some(evl) = enum_def.variant_list() {
evl.variants()
.map(|v| {
(
2018-12-28 19:34:58 +01:00
v.name().map(|n| n.as_name()).unwrap_or_else(Name::missing),
Arc::new(VariantData::new(v.flavor())),
)
})
.collect()
} else {
Vec::new()
};
EnumData { name, variants }
}
}
/// A single field of an enum variant or struct
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StructField {
2018-12-28 19:34:58 +01:00
name: Name,
type_ref: TypeRef,
}
2018-12-25 15:15:40 +01:00
impl StructField {
2018-12-28 19:34:58 +01:00
pub fn name(&self) -> Name {
2018-12-25 15:15:40 +01:00
self.name.clone()
}
pub fn type_ref(&self) -> &TypeRef {
&self.type_ref
2018-12-25 15:15:40 +01:00
}
}
/// 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 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()
2018-12-28 19:34:58 +01:00
.find(|f| f.name == *field_name)
.map(|f| &f.type_ref)
2018-12-25 13:54:38 +01:00
}
pub fn fields(&self) -> &[StructField] {
match *self {
VariantData::Struct(ref fields) | VariantData::Tuple(ref fields) => fields,
_ => &[],
}
}
pub fn is_struct(&self) -> bool {
2019-01-08 12:23:00 +01:00
match self {
VariantData::Struct(..) => true,
_ => false,
}
}
pub fn is_tuple(&self) -> bool {
2019-01-08 12:23:00 +01:00
match self {
VariantData::Tuple(..) => true,
_ => false,
}
}
pub fn is_unit(&self) -> bool {
2019-01-08 12:23:00 +01:00
match self {
VariantData::Unit => true,
_ => false,
}
}
}