mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 04:23:25 +00:00
Merge #456
456: Move adt to code_model_api r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
5603237c06
5 changed files with 131 additions and 124 deletions
|
@ -1,93 +1,60 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use ra_db::Cancelable;
|
||||
use ra_syntax::ast::{self, NameOwner, StructFlavor};
|
||||
use ra_syntax::ast::{self, NameOwner, StructFlavor, AstNode};
|
||||
|
||||
use crate::{
|
||||
DefId, Name, AsName,
|
||||
db::HirDatabase,
|
||||
DefId, Name, AsName, Struct, Enum, VariantData, StructField, HirDatabase, DefKind,
|
||||
type_ref::TypeRef,
|
||||
};
|
||||
|
||||
pub struct Struct {
|
||||
def_id: DefId,
|
||||
}
|
||||
|
||||
impl Struct {
|
||||
pub(crate) fn new(def_id: DefId) -> Self {
|
||||
Struct { def_id }
|
||||
}
|
||||
|
||||
pub fn def_id(&self) -> DefId {
|
||||
self.def_id
|
||||
}
|
||||
|
||||
pub fn variant_data(&self, db: &impl HirDatabase) -> Cancelable<Arc<VariantData>> {
|
||||
Ok(db.struct_data(self.def_id)?.variant_data.clone())
|
||||
}
|
||||
|
||||
pub fn struct_data(&self, db: &impl HirDatabase) -> Cancelable<Arc<StructData>> {
|
||||
Ok(db.struct_data(self.def_id)?)
|
||||
}
|
||||
|
||||
pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> {
|
||||
Ok(db.struct_data(self.def_id)?.name.clone())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct StructData {
|
||||
name: Option<Name>,
|
||||
variant_data: Arc<VariantData>,
|
||||
pub(crate) name: Option<Name>,
|
||||
pub(crate) variant_data: Arc<VariantData>,
|
||||
}
|
||||
|
||||
impl StructData {
|
||||
pub(crate) fn new(struct_def: &ast::StructDef) -> StructData {
|
||||
fn new(struct_def: &ast::StructDef) -> StructData {
|
||||
let name = struct_def.name().map(|n| n.as_name());
|
||||
let variant_data = VariantData::new(struct_def.flavor());
|
||||
let variant_data = Arc::new(variant_data);
|
||||
StructData { name, variant_data }
|
||||
}
|
||||
|
||||
pub fn name(&self) -> Option<&Name> {
|
||||
self.name.as_ref()
|
||||
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)))
|
||||
}
|
||||
|
||||
pub fn variant_data(&self) -> &Arc<VariantData> {
|
||||
&self.variant_data
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Enum {
|
||||
def_id: DefId,
|
||||
}
|
||||
|
||||
impl Enum {
|
||||
pub(crate) fn new(def_id: DefId) -> Self {
|
||||
Enum { def_id }
|
||||
}
|
||||
|
||||
pub fn def_id(&self) -> DefId {
|
||||
self.def_id
|
||||
}
|
||||
|
||||
pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> {
|
||||
Ok(db.enum_data(self.def_id)?.name.clone())
|
||||
}
|
||||
|
||||
pub fn variants(&self, db: &impl HirDatabase) -> Cancelable<Vec<(Name, Arc<VariantData>)>> {
|
||||
Ok(db.enum_data(self.def_id)?.variants.clone())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct EnumData {
|
||||
name: Option<Name>,
|
||||
variants: Vec<(Name, Arc<VariantData>)>,
|
||||
pub(crate) name: Option<Name>,
|
||||
pub(crate) variants: Vec<(Name, Arc<VariantData>)>,
|
||||
}
|
||||
|
||||
impl EnumData {
|
||||
pub(crate) fn new(enum_def: &ast::EnumDef) -> Self {
|
||||
fn new(enum_def: &ast::EnumDef) -> Self {
|
||||
let name = enum_def.name().map(|n| n.as_name());
|
||||
let variants = if let Some(evl) = enum_def.variant_list() {
|
||||
evl.variants()
|
||||
|
@ -103,34 +70,21 @@ impl EnumData {
|
|||
};
|
||||
EnumData { name, variants }
|
||||
}
|
||||
}
|
||||
|
||||
/// A single field of an enum variant or struct
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct StructField {
|
||||
name: Name,
|
||||
type_ref: TypeRef,
|
||||
}
|
||||
|
||||
impl StructField {
|
||||
pub fn name(&self) -> Name {
|
||||
self.name.clone()
|
||||
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)))
|
||||
}
|
||||
pub fn type_ref(&self) -> &TypeRef {
|
||||
&self.type_ref
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 new(flavor: StructFlavor) -> Self {
|
||||
fn new(flavor: StructFlavor) -> Self {
|
||||
match flavor {
|
||||
StructFlavor::Tuple(fl) => {
|
||||
let fields = fl
|
||||
|
@ -160,32 +114,7 @@ impl VariantData {
|
|||
pub(crate) fn get_field_type_ref(&self, field_name: &Name) -> Option<&TypeRef> {
|
||||
self.fields()
|
||||
.iter()
|
||||
.find(|f| f.name == *field_name)
|
||||
.map(|f| &f.type_ref)
|
||||
}
|
||||
|
||||
pub fn fields(&self) -> &[StructField] {
|
||||
match *self {
|
||||
VariantData::Struct(ref fields) | VariantData::Tuple(ref 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,
|
||||
}
|
||||
.find(|f| f.name() == field_name)
|
||||
.map(|f| f.type_ref())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,15 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use relative_path::RelativePathBuf;
|
||||
use ra_db::{CrateId, Cancelable, FileId};
|
||||
use ra_syntax::{ast, TreePtr, SyntaxNode};
|
||||
|
||||
use crate::{Name, db::HirDatabase, DefId, Path, PerNs, nameres::ModuleScope};
|
||||
use crate::{
|
||||
Name, DefId, Path, PerNs,
|
||||
type_ref::TypeRef,
|
||||
nameres::ModuleScope,
|
||||
db::HirDatabase,
|
||||
};
|
||||
|
||||
/// hir::Crate describes a single crate. It's the main inteface with which
|
||||
/// crate's dependencies interact. Mostly, it should be just a proxy for the
|
||||
|
@ -111,3 +118,92 @@ impl Module {
|
|||
self.problems_impl(db)
|
||||
}
|
||||
}
|
||||
|
||||
/// 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,
|
||||
}
|
||||
|
||||
impl StructField {
|
||||
pub fn name(&self) -> &Name {
|
||||
&self.name
|
||||
}
|
||||
pub fn type_ref(&self) -> &TypeRef {
|
||||
&self.type_ref
|
||||
}
|
||||
}
|
||||
|
||||
/// 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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Struct {
|
||||
pub(crate) def_id: DefId,
|
||||
}
|
||||
|
||||
impl Struct {
|
||||
pub fn def_id(&self) -> DefId {
|
||||
self.def_id
|
||||
}
|
||||
|
||||
pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> {
|
||||
Ok(db.struct_data(self.def_id)?.name.clone())
|
||||
}
|
||||
|
||||
pub fn variant_data(&self, db: &impl HirDatabase) -> Cancelable<Arc<VariantData>> {
|
||||
Ok(db.struct_data(self.def_id)?.variant_data.clone())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Enum {
|
||||
pub(crate) def_id: DefId,
|
||||
}
|
||||
|
||||
impl Enum {
|
||||
pub fn def_id(&self) -> DefId {
|
||||
self.def_id
|
||||
}
|
||||
|
||||
pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> {
|
||||
Ok(db.enum_data(self.def_id)?.name.clone())
|
||||
}
|
||||
|
||||
pub fn variants(&self, db: &impl HirDatabase) -> Cancelable<Vec<(Name, Arc<VariantData>)>> {
|
||||
Ok(db.enum_data(self.def_id)?.variants.clone())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,12 +38,12 @@ pub trait HirDatabase: SyntaxDatabase
|
|||
|
||||
fn struct_data(def_id: DefId) -> Cancelable<Arc<StructData>> {
|
||||
type StructDataQuery;
|
||||
use fn query_definitions::struct_data;
|
||||
use fn crate::adt::StructData::struct_data_query;
|
||||
}
|
||||
|
||||
fn enum_data(def_id: DefId) -> Cancelable<Arc<EnumData>> {
|
||||
type EnumDataQuery;
|
||||
use fn query_definitions::enum_data;
|
||||
use fn crate::adt::EnumData::enum_data_query;
|
||||
}
|
||||
|
||||
fn infer(def_id: DefId) -> Cancelable<Arc<InferenceResult>> {
|
||||
|
|
|
@ -50,7 +50,6 @@ pub use self::{
|
|||
module_tree::ModuleId,
|
||||
nameres::{ItemMap, PerNs, Namespace, Resolution},
|
||||
function::{Function, FnSignature, FnScopes, ScopesWithSyntaxMapping},
|
||||
adt::{Struct, Enum},
|
||||
ty::Ty,
|
||||
impl_block::{ImplBlock, ImplItem},
|
||||
};
|
||||
|
@ -60,6 +59,7 @@ pub use self::function::FnSignatureInfo;
|
|||
pub use self::code_model_api::{
|
||||
Crate, CrateDependency,
|
||||
Module, ModuleSource, Problem,
|
||||
Struct, Enum, VariantData, StructField,
|
||||
};
|
||||
|
||||
pub enum Def {
|
||||
|
|
|
@ -11,13 +11,12 @@ use ra_syntax::{
|
|||
use ra_db::{SourceRootId, Cancelable,};
|
||||
|
||||
use crate::{
|
||||
SourceFileItems, SourceItemId, DefKind, DefId, HirFileId, ModuleSource,
|
||||
SourceFileItems, SourceItemId, DefId, HirFileId, ModuleSource,
|
||||
MacroCallLoc,
|
||||
db::HirDatabase,
|
||||
function::FnScopes,
|
||||
module_tree::ModuleId,
|
||||
nameres::{InputModuleItems, ItemMap, Resolver},
|
||||
adt::{StructData, EnumData},
|
||||
};
|
||||
|
||||
pub(super) fn fn_scopes(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Arc<FnScopes>> {
|
||||
|
@ -26,23 +25,6 @@ pub(super) fn fn_scopes(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Arc<
|
|||
Ok(Arc::new(res))
|
||||
}
|
||||
|
||||
pub(super) fn struct_data(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)))
|
||||
}
|
||||
|
||||
pub(super) fn enum_data(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)))
|
||||
}
|
||||
|
||||
pub(super) fn file_items(db: &impl HirDatabase, file_id: HirFileId) -> Arc<SourceFileItems> {
|
||||
let source_file = db.hir_source_file(file_id);
|
||||
let res = SourceFileItems::new(file_id, &source_file);
|
||||
|
|
Loading…
Reference in a new issue