add union to code_model

This commit is contained in:
Aleksey Kladov 2019-05-23 20:18:47 +03:00
parent eef24bddc9
commit 5d54aa6781
13 changed files with 102 additions and 26 deletions

View file

@ -71,6 +71,7 @@ pub enum ModuleDef {
Module(Module),
Function(Function),
Struct(Struct),
Union(Union),
Enum(Enum),
// Can't be directly declared, but can be imported.
EnumVariant(EnumVariant),
@ -83,6 +84,7 @@ impl_froms!(
ModuleDef: Module,
Function,
Struct,
Union,
Enum,
EnumVariant,
Const,
@ -325,6 +327,42 @@ impl Docs for Struct {
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Union {
pub(crate) id: StructId,
}
impl Union {
pub fn source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::StructDef>) {
self.id.source(db)
}
pub fn name(&self, db: &impl HirDatabase) -> Option<Name> {
db.struct_data(Struct { id: self.id }).name.clone()
}
pub fn module(&self, db: &impl HirDatabase) -> Module {
self.id.module(db)
}
// FIXME move to a more general type
/// Builds a resolver for type references inside this union.
pub(crate) fn resolver(&self, db: &impl HirDatabase) -> Resolver {
// take the outer scope...
let r = self.module(db).resolver(db);
// ...and add generic params, if present
let p = self.generic_params(db);
let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
r
}
}
impl Docs for Union {
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
docs_from_ast(&*self.source(db).1)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Enum {
pub(crate) id: EnumId,