2019-01-12 20:27:35 +00:00
|
|
|
//! This module contains the implementation details of the HIR for ADTs, i.e.
|
|
|
|
//! structs and enums (and unions).
|
|
|
|
|
2018-12-24 20:00:14 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2019-10-31 13:40:36 +00:00
|
|
|
use hir_def::adt::VariantData;
|
2018-12-24 18:07:48 +00:00
|
|
|
|
|
|
|
use crate::{
|
2019-10-31 13:40:36 +00:00
|
|
|
db::{DefDatabase, HirDatabase},
|
|
|
|
EnumVariant, Module, Name, Struct, StructField,
|
2018-12-24 18:07:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
impl Struct {
|
2019-07-05 16:02:32 +00:00
|
|
|
pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
|
2019-10-31 13:40:36 +00:00
|
|
|
db.struct_data(self.id).variant_data.clone()
|
2018-12-25 12:31:30 +00:00
|
|
|
}
|
2018-12-24 18:07:48 +00:00
|
|
|
}
|
2019-01-25 17:32:34 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
|
|
|
pub enum VariantDef {
|
|
|
|
Struct(Struct),
|
|
|
|
EnumVariant(EnumVariant),
|
|
|
|
}
|
|
|
|
impl_froms!(VariantDef: Struct, EnumVariant);
|
|
|
|
|
|
|
|
impl VariantDef {
|
2019-07-12 16:56:18 +00:00
|
|
|
pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> {
|
|
|
|
match self {
|
|
|
|
VariantDef::Struct(it) => it.fields(db),
|
|
|
|
VariantDef::EnumVariant(it) => it.fields(db),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-09 11:59:47 +00:00
|
|
|
pub fn field(self, db: &impl HirDatabase, name: &Name) -> Option<StructField> {
|
2019-01-25 17:32:34 +00:00
|
|
|
match self {
|
|
|
|
VariantDef::Struct(it) => it.field(db, name),
|
|
|
|
VariantDef::EnumVariant(it) => it.field(db, name),
|
|
|
|
}
|
|
|
|
}
|
2019-10-09 11:59:47 +00:00
|
|
|
|
|
|
|
pub fn module(self, db: &impl HirDatabase) -> Module {
|
|
|
|
match self {
|
|
|
|
VariantDef::Struct(it) => it.module(db),
|
|
|
|
VariantDef::EnumVariant(it) => it.module(db),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-23 12:37:04 +00:00
|
|
|
pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
|
2019-01-25 17:32:34 +00:00
|
|
|
match self {
|
|
|
|
VariantDef::Struct(it) => it.variant_data(db),
|
|
|
|
VariantDef::EnumVariant(it) => it.variant_data(db),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|