2019-01-12 14:27:35 -06:00
|
|
|
//! This module contains the implementation details of the HIR for ADTs, i.e.
|
|
|
|
//! structs and enums (and unions).
|
|
|
|
|
2018-12-24 14:00:14 -06:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2019-01-08 09:01:19 -06:00
|
|
|
use ra_syntax::{
|
|
|
|
SyntaxNode,
|
|
|
|
ast::{self, NameOwner, StructFlavor, AstNode}
|
|
|
|
};
|
2018-12-24 12:07:48 -06:00
|
|
|
|
|
|
|
use crate::{
|
2019-01-24 08:54:18 -06:00
|
|
|
DefId, DefLoc, Name, AsName, Struct, Enum, EnumVariant, Module, HirFileId,
|
2019-01-09 19:07:42 -06:00
|
|
|
HirDatabase, DefKind,
|
2019-01-08 09:01:19 -06:00
|
|
|
SourceItemId,
|
2018-12-25 14:40:33 -06:00
|
|
|
type_ref::TypeRef,
|
2019-01-24 10:29:44 -06:00
|
|
|
ids::ItemLoc,
|
2018-12-24 12:07:48 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
impl Struct {
|
2019-01-24 08:54:18 -06:00
|
|
|
pub(crate) fn from_ast(
|
|
|
|
db: &impl HirDatabase,
|
|
|
|
module: Module,
|
|
|
|
file_id: HirFileId,
|
|
|
|
ast: &ast::StructDef,
|
|
|
|
) -> Struct {
|
2019-01-24 10:29:44 -06:00
|
|
|
let loc = ItemLoc::from_ast(db, module, file_id, ast);
|
|
|
|
let id = db.as_ref().structs.loc2id(&loc);
|
2019-01-24 08:54:18 -06:00
|
|
|
Struct { id }
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|
2019-01-09 09:46:02 -06:00
|
|
|
|
2019-01-15 11:43:37 -06:00
|
|
|
pub(crate) fn variant_data(&self, db: &impl HirDatabase) -> Arc<VariantData> {
|
2019-01-24 08:54:18 -06:00
|
|
|
db.struct_data((*self).into()).variant_data.clone()
|
2019-01-09 09:46:02 -06:00
|
|
|
}
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|
|
|
|
|
2019-01-24 09:56:38 -06:00
|
|
|
impl Enum {
|
|
|
|
pub(crate) fn from_ast(
|
|
|
|
db: &impl HirDatabase,
|
|
|
|
module: Module,
|
|
|
|
file_id: HirFileId,
|
|
|
|
ast: &ast::EnumDef,
|
|
|
|
) -> Enum {
|
2019-01-24 10:29:44 -06:00
|
|
|
let loc = ItemLoc::from_ast(db, module, file_id, ast);
|
|
|
|
let id = db.as_ref().enums.loc2id(&loc);
|
2019-01-24 09:56:38 -06:00
|
|
|
Enum { id }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-24 12:07:48 -06:00
|
|
|
#[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
|
|
|
|
2019-01-24 08:54:18 -06:00
|
|
|
pub(crate) fn struct_data_query(db: &impl HirDatabase, struct_: Struct) -> Arc<StructData> {
|
|
|
|
let (_, struct_def) = struct_.source(db);
|
|
|
|
Arc::new(StructData::new(&*struct_def))
|
2019-01-08 06:38:29 -06:00
|
|
|
}
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|
|
|
|
|
2019-01-08 09:01:19 -06:00
|
|
|
fn get_def_id(
|
|
|
|
db: &impl HirDatabase,
|
2019-01-24 09:56:38 -06:00
|
|
|
module: Module,
|
|
|
|
file_id: HirFileId,
|
2019-01-08 09:01:19 -06:00
|
|
|
node: &SyntaxNode,
|
|
|
|
expected_kind: DefKind,
|
|
|
|
) -> DefId {
|
|
|
|
let file_items = db.file_items(file_id);
|
|
|
|
|
|
|
|
let item_id = file_items.id_of(file_id, node);
|
|
|
|
let source_item_id = SourceItemId {
|
2019-01-24 09:56:38 -06:00
|
|
|
file_id,
|
2019-01-08 09:01:19 -06:00
|
|
|
item_id: Some(item_id),
|
|
|
|
};
|
|
|
|
let loc = DefLoc {
|
2019-01-24 09:56:38 -06:00
|
|
|
module,
|
2019-01-08 09:01:19 -06:00
|
|
|
kind: expected_kind,
|
2019-01-12 14:27:35 -06:00
|
|
|
source_item_id,
|
2019-01-08 09:01:19 -06:00
|
|
|
};
|
|
|
|
loc.id(db)
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct EnumData {
|
2019-01-08 06:22:57 -06:00
|
|
|
pub(crate) name: Option<Name>,
|
2019-01-09 19:07:42 -06:00
|
|
|
pub(crate) variants: Vec<(Name, EnumVariant)>,
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EnumData {
|
2019-01-09 19:07:42 -06:00
|
|
|
fn new(enum_def: &ast::EnumDef, variants: Vec<(Name, EnumVariant)>) -> Self {
|
2018-12-28 12:34:58 -06:00
|
|
|
let name = enum_def.name().map(|n| n.as_name());
|
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
|
|
|
|
2019-01-24 09:56:38 -06:00
|
|
|
pub(crate) fn enum_data_query(db: &impl HirDatabase, e: Enum) -> Arc<EnumData> {
|
|
|
|
let (file_id, enum_def) = e.source(db);
|
|
|
|
let module = e.module(db);
|
2019-01-09 19:07:42 -06:00
|
|
|
let variants = if let Some(vl) = enum_def.variant_list() {
|
2019-01-08 09:01:19 -06:00
|
|
|
vl.variants()
|
2019-01-09 19:07:42 -06:00
|
|
|
.filter_map(|variant_def| {
|
|
|
|
let name = variant_def.name().map(|n| n.as_name());
|
|
|
|
|
|
|
|
name.map(|n| {
|
2019-01-24 09:56:38 -06:00
|
|
|
let def_id = get_def_id(
|
|
|
|
db,
|
|
|
|
module,
|
|
|
|
file_id,
|
|
|
|
variant_def.syntax(),
|
|
|
|
DefKind::EnumVariant,
|
|
|
|
);
|
2019-01-09 19:07:42 -06:00
|
|
|
(n, EnumVariant::new(def_id))
|
|
|
|
})
|
2019-01-08 09:01:19 -06:00
|
|
|
})
|
|
|
|
.collect()
|
2019-01-09 19:07:42 -06:00
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
};
|
2019-01-24 09:56:38 -06:00
|
|
|
Arc::new(EnumData::new(&*enum_def, variants))
|
2019-01-08 09:01:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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 09:43:25 -06:00
|
|
|
) -> Arc<EnumVariantData> {
|
2019-01-08 09:01:19 -06:00
|
|
|
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");
|
2019-01-24 09:56:38 -06:00
|
|
|
let enum_def = variant_def.parent_enum();
|
|
|
|
let e = Enum::from_ast(db, def_loc.module, def_loc.source_item_id.file_id, enum_def);
|
|
|
|
|
|
|
|
Arc::new(EnumVariantData::new(variant_def, e))
|
2019-01-08 06:38:29 -06:00
|
|
|
}
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|
|
|
|
|
2019-01-09 09:46:02 -06: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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-09 09:46:02 -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
|
|
|
}
|