2018-12-24 14:00:14 -06:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2018-12-25 06:31:30 -06:00
|
|
|
use ra_syntax::{SmolStr, ast::{self, NameOwner, StructFlavor}};
|
2018-12-24 12:07:48 -06:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
DefId, Cancelable,
|
|
|
|
db::{HirDatabase},
|
2018-12-25 06:31:30 -06:00
|
|
|
module::Module,
|
2018-12-24 12:07:48 -06:00
|
|
|
ty::{Ty},
|
|
|
|
};
|
|
|
|
|
|
|
|
pub struct Struct {
|
|
|
|
def_id: DefId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Struct {
|
|
|
|
pub(crate) fn new(def_id: DefId) -> Self {
|
|
|
|
Struct { def_id }
|
|
|
|
}
|
|
|
|
|
2018-12-24 14:00:14 -06:00
|
|
|
pub fn def_id(&self) -> DefId {
|
|
|
|
self.def_id
|
|
|
|
}
|
|
|
|
|
2018-12-25 06:54:38 -06:00
|
|
|
pub fn variant_data(&self, db: &impl HirDatabase) -> Cancelable<Arc<VariantData>> {
|
|
|
|
Ok(db.struct_data(self.def_id)?.variant_data.clone())
|
|
|
|
}
|
|
|
|
|
2018-12-24 14:00:14 -06:00
|
|
|
pub fn struct_data(&self, db: &impl HirDatabase) -> Cancelable<Arc<StructData>> {
|
|
|
|
Ok(db.struct_data(self.def_id)?)
|
|
|
|
}
|
|
|
|
|
2018-12-25 10:55:50 -06:00
|
|
|
pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<SmolStr>> {
|
2018-12-24 12:07:48 -06:00
|
|
|
Ok(db.struct_data(self.def_id)?.name.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct StructData {
|
2018-12-25 10:55:50 -06:00
|
|
|
name: Option<SmolStr>,
|
2018-12-24 14:00:14 -06:00
|
|
|
variant_data: Arc<VariantData>,
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl StructData {
|
2018-12-25 06:31:30 -06:00
|
|
|
pub(crate) fn new(
|
|
|
|
db: &impl HirDatabase,
|
|
|
|
module: &Module,
|
|
|
|
struct_def: ast::StructDef,
|
|
|
|
) -> Cancelable<StructData> {
|
2018-12-25 10:55:50 -06:00
|
|
|
let name = struct_def.name().map(|n| n.text());
|
2018-12-25 06:31:30 -06:00
|
|
|
let variant_data = VariantData::new(db, module, struct_def.flavor())?;
|
2018-12-24 14:00:14 -06:00
|
|
|
let variant_data = Arc::new(variant_data);
|
2018-12-25 06:31:30 -06:00
|
|
|
Ok(StructData { name, variant_data })
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|
2018-12-24 14:00:14 -06:00
|
|
|
|
2018-12-25 10:55:50 -06:00
|
|
|
pub fn name(&self) -> Option<&SmolStr> {
|
|
|
|
self.name.as_ref()
|
2018-12-24 14:00:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn variant_data(&self) -> &Arc<VariantData> {
|
|
|
|
&self.variant_data
|
|
|
|
}
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Enum {
|
|
|
|
def_id: DefId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Enum {
|
|
|
|
pub(crate) fn new(def_id: DefId) -> Self {
|
|
|
|
Enum { def_id }
|
|
|
|
}
|
|
|
|
|
2018-12-24 14:00:14 -06:00
|
|
|
pub fn def_id(&self) -> DefId {
|
|
|
|
self.def_id
|
|
|
|
}
|
|
|
|
|
2018-12-25 10:55:50 -06:00
|
|
|
pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<SmolStr>> {
|
2018-12-24 12:07:48 -06:00
|
|
|
Ok(db.enum_data(self.def_id)?.name.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct EnumData {
|
2018-12-25 10:55:50 -06:00
|
|
|
name: Option<SmolStr>,
|
2018-12-24 14:00:14 -06:00
|
|
|
variants: Vec<(SmolStr, Arc<VariantData>)>,
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EnumData {
|
2018-12-25 06:31:30 -06:00
|
|
|
pub(crate) fn new(
|
|
|
|
db: &impl HirDatabase,
|
|
|
|
module: &Module,
|
|
|
|
enum_def: ast::EnumDef,
|
|
|
|
) -> Cancelable<Self> {
|
2018-12-25 10:55:50 -06:00
|
|
|
let name = enum_def.name().map(|n| n.text());
|
2018-12-25 06:31:30 -06:00
|
|
|
let variants = if let Some(evl) = enum_def.variant_list() {
|
|
|
|
evl.variants()
|
|
|
|
.map(|v| {
|
|
|
|
Ok((
|
|
|
|
v.name()
|
|
|
|
.map(|n| n.text())
|
|
|
|
.unwrap_or_else(|| SmolStr::new("[error]")),
|
|
|
|
Arc::new(VariantData::new(db, module, v.flavor())?),
|
|
|
|
))
|
|
|
|
})
|
|
|
|
.collect::<Cancelable<_>>()?
|
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
};
|
|
|
|
Ok(EnumData { name, variants })
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A single field of an enum variant or struct
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct StructField {
|
|
|
|
name: SmolStr,
|
|
|
|
ty: Ty,
|
|
|
|
}
|
|
|
|
|
2018-12-25 08:15:40 -06:00
|
|
|
impl StructField {
|
|
|
|
pub fn name(&self) -> SmolStr {
|
|
|
|
self.name.clone()
|
|
|
|
}
|
|
|
|
pub fn ty(&self) -> Ty {
|
|
|
|
self.ty.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-24 12:07:48 -06: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 {
|
2018-12-25 06:31:30 -06:00
|
|
|
pub fn new(db: &impl HirDatabase, module: &Module, flavor: StructFlavor) -> Cancelable<Self> {
|
|
|
|
Ok(match flavor {
|
|
|
|
StructFlavor::Tuple(fl) => {
|
|
|
|
let fields = fl
|
|
|
|
.fields()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, fd)| {
|
|
|
|
Ok(StructField {
|
|
|
|
name: SmolStr::new(i.to_string()),
|
2018-12-25 14:14:13 -06:00
|
|
|
ty: Ty::from_ast_opt(db, &module, fd.type_ref())?,
|
2018-12-25 06:31:30 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect::<Cancelable<_>>()?;
|
|
|
|
VariantData::Tuple(fields)
|
|
|
|
}
|
|
|
|
StructFlavor::Named(fl) => {
|
|
|
|
let fields = fl
|
|
|
|
.fields()
|
|
|
|
.map(|fd| {
|
|
|
|
Ok(StructField {
|
|
|
|
name: fd
|
|
|
|
.name()
|
|
|
|
.map(|n| n.text())
|
|
|
|
.unwrap_or_else(|| SmolStr::new("[error]")),
|
2018-12-25 14:14:13 -06:00
|
|
|
ty: Ty::from_ast_opt(db, &module, fd.type_ref())?,
|
2018-12-25 06:31:30 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect::<Cancelable<_>>()?;
|
|
|
|
VariantData::Struct(fields)
|
|
|
|
}
|
|
|
|
StructFlavor::Unit => VariantData::Unit,
|
|
|
|
})
|
|
|
|
}
|
2018-12-25 06:54:38 -06:00
|
|
|
|
|
|
|
pub(crate) fn get_field_ty(&self, field_name: &str) -> Option<Ty> {
|
2018-12-25 08:15:40 -06:00
|
|
|
self.fields()
|
|
|
|
.iter()
|
|
|
|
.find(|f| f.name == field_name)
|
|
|
|
.map(|f| f.ty.clone())
|
2018-12-25 06:54:38 -06:00
|
|
|
}
|
|
|
|
|
2018-12-24 12:07:48 -06:00
|
|
|
pub fn fields(&self) -> &[StructField] {
|
|
|
|
match *self {
|
|
|
|
VariantData::Struct(ref fields) | VariantData::Tuple(ref fields) => fields,
|
|
|
|
_ => &[],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn is_struct(&self) -> bool {
|
|
|
|
if let VariantData::Struct(..) = *self {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn is_tuple(&self) -> bool {
|
|
|
|
if let VariantData::Tuple(..) = *self {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn is_unit(&self) -> bool {
|
|
|
|
if let VariantData::Unit = *self {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|