2018-12-24 14:00:14 -06:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2019-01-08 06:38:29 -06:00
|
|
|
use ra_db::Cancelable;
|
|
|
|
use ra_syntax::ast::{self, NameOwner, StructFlavor, AstNode};
|
2018-12-24 12:07:48 -06:00
|
|
|
|
|
|
|
use crate::{
|
2019-01-08 06:38:29 -06:00
|
|
|
DefId, Name, AsName, Struct, Enum, VariantData, StructField, HirDatabase, DefKind,
|
2018-12-25 14:40:33 -06:00
|
|
|
type_ref::TypeRef,
|
2018-12-24 12:07:48 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
impl Struct {
|
|
|
|
pub(crate) fn new(def_id: DefId) -> Self {
|
|
|
|
Struct { def_id }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
|
|
|
|
|
|
pub(crate) fn struct_data_query(
|
|
|
|
db: &impl HirDatabase,
|
|
|
|
def_id: DefId,
|
|
|
|
) -> Cancelable<Arc<StructData>> {
|
|
|
|
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");
|
|
|
|
Ok(Arc::new(StructData::new(struct_def)))
|
|
|
|
}
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Enum {
|
|
|
|
pub(crate) fn new(def_id: DefId) -> Self {
|
|
|
|
Enum { def_id }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct EnumData {
|
2019-01-08 06:22:57 -06:00
|
|
|
pub(crate) name: Option<Name>,
|
|
|
|
pub(crate) variants: Vec<(Name, Arc<VariantData>)>,
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EnumData {
|
2019-01-08 06:38:29 -06:00
|
|
|
fn new(enum_def: &ast::EnumDef) -> Self {
|
2018-12-28 12:34:58 -06:00
|
|
|
let name = enum_def.name().map(|n| n.as_name());
|
2018-12-25 06:31:30 -06:00
|
|
|
let variants = if let Some(evl) = enum_def.variant_list() {
|
|
|
|
evl.variants()
|
|
|
|
.map(|v| {
|
2018-12-25 14:40:33 -06:00
|
|
|
(
|
2018-12-28 12:34:58 -06:00
|
|
|
v.name().map(|n| n.as_name()).unwrap_or_else(Name::missing),
|
2018-12-25 14:40:33 -06:00
|
|
|
Arc::new(VariantData::new(v.flavor())),
|
|
|
|
)
|
2018-12-25 06:31:30 -06:00
|
|
|
})
|
2018-12-25 14:40:33 -06:00
|
|
|
.collect()
|
2018-12-25 06:31:30 -06:00
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
};
|
2018-12-25 14:40:33 -06:00
|
|
|
EnumData { name, variants }
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|
2019-01-08 06:38:29 -06:00
|
|
|
|
|
|
|
pub(crate) fn enum_data_query(
|
|
|
|
db: &impl HirDatabase,
|
|
|
|
def_id: DefId,
|
|
|
|
) -> Cancelable<Arc<EnumData>> {
|
|
|
|
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");
|
|
|
|
Ok(Arc::new(EnumData::new(enum_def)))
|
|
|
|
}
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl VariantData {
|
2019-01-08 06:40:02 -06:00
|
|
|
fn new(flavor: StructFlavor) -> Self {
|
2018-12-25 14:40:33 -06:00
|
|
|
match flavor {
|
2018-12-25 06:31:30 -06:00
|
|
|
StructFlavor::Tuple(fl) => {
|
|
|
|
let fields = fl
|
|
|
|
.fields()
|
|
|
|
.enumerate()
|
2018-12-25 14:40:33 -06:00
|
|
|
.map(|(i, fd)| StructField {
|
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();
|
2018-12-25 06:31:30 -06:00
|
|
|
VariantData::Tuple(fields)
|
|
|
|
}
|
|
|
|
StructFlavor::Named(fl) => {
|
|
|
|
let fields = fl
|
|
|
|
.fields()
|
2018-12-25 14:40:33 -06:00
|
|
|
.map(|fd| StructField {
|
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();
|
2018-12-25 06:31:30 -06:00
|
|
|
VariantData::Struct(fields)
|
|
|
|
}
|
|
|
|
StructFlavor::Unit => VariantData::Unit,
|
2018-12-25 14:40:33 -06:00
|
|
|
}
|
2018-12-25 06:31:30 -06:00
|
|
|
}
|
2018-12-25 06:54:38 -06:00
|
|
|
|
2018-12-28 12:34:58 -06:00
|
|
|
pub(crate) fn get_field_type_ref(&self, field_name: &Name) -> Option<&TypeRef> {
|
2018-12-25 08:15:40 -06:00
|
|
|
self.fields()
|
|
|
|
.iter()
|
2019-01-08 06:27:00 -06:00
|
|
|
.find(|f| f.name() == field_name)
|
|
|
|
.map(|f| f.type_ref())
|
2018-12-25 06:54:38 -06:00
|
|
|
}
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|