diff options
Diffstat (limited to 'crates/ra_hir/src/adt.rs')
-rw-r--r-- | crates/ra_hir/src/adt.rs | 69 |
1 files changed, 62 insertions, 7 deletions
diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index ee270ac45..b44f59f0b 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs | |||
@@ -1,10 +1,11 @@ | |||
1 | use std::sync::Arc; | 1 | use std::sync::Arc; |
2 | 2 | ||
3 | use ra_syntax::{SmolStr, ast::{self, NameOwner}}; | 3 | use ra_syntax::{SmolStr, ast::{self, NameOwner, StructFlavor}}; |
4 | 4 | ||
5 | use crate::{ | 5 | use crate::{ |
6 | DefId, Cancelable, | 6 | DefId, Cancelable, |
7 | db::{HirDatabase}, | 7 | db::{HirDatabase}, |
8 | module::Module, | ||
8 | ty::{Ty}, | 9 | ty::{Ty}, |
9 | }; | 10 | }; |
10 | 11 | ||
@@ -37,14 +38,18 @@ pub struct StructData { | |||
37 | } | 38 | } |
38 | 39 | ||
39 | impl StructData { | 40 | impl StructData { |
40 | pub(crate) fn new(struct_def: ast::StructDef) -> StructData { | 41 | pub(crate) fn new( |
42 | db: &impl HirDatabase, | ||
43 | module: &Module, | ||
44 | struct_def: ast::StructDef, | ||
45 | ) -> Cancelable<StructData> { | ||
41 | let name = struct_def | 46 | let name = struct_def |
42 | .name() | 47 | .name() |
43 | .map(|n| n.text()) | 48 | .map(|n| n.text()) |
44 | .unwrap_or(SmolStr::new("[error]")); | 49 | .unwrap_or(SmolStr::new("[error]")); |
45 | let variant_data = VariantData::Unit; // TODO implement this | 50 | let variant_data = VariantData::new(db, module, struct_def.flavor())?; |
46 | let variant_data = Arc::new(variant_data); | 51 | let variant_data = Arc::new(variant_data); |
47 | StructData { name, variant_data } | 52 | Ok(StructData { name, variant_data }) |
48 | } | 53 | } |
49 | 54 | ||
50 | pub fn name(&self) -> &SmolStr { | 55 | pub fn name(&self) -> &SmolStr { |
@@ -81,13 +86,30 @@ pub struct EnumData { | |||
81 | } | 86 | } |
82 | 87 | ||
83 | impl EnumData { | 88 | impl EnumData { |
84 | pub(crate) fn new(enum_def: ast::EnumDef) -> Self { | 89 | pub(crate) fn new( |
90 | db: &impl HirDatabase, | ||
91 | module: &Module, | ||
92 | enum_def: ast::EnumDef, | ||
93 | ) -> Cancelable<Self> { | ||
85 | let name = enum_def | 94 | let name = enum_def |
86 | .name() | 95 | .name() |
87 | .map(|n| n.text()) | 96 | .map(|n| n.text()) |
88 | .unwrap_or(SmolStr::new("[error]")); | 97 | .unwrap_or(SmolStr::new("[error]")); |
89 | let variants = Vec::new(); // TODO implement this | 98 | let variants = if let Some(evl) = enum_def.variant_list() { |
90 | EnumData { name, variants } | 99 | evl.variants() |
100 | .map(|v| { | ||
101 | Ok(( | ||
102 | v.name() | ||
103 | .map(|n| n.text()) | ||
104 | .unwrap_or_else(|| SmolStr::new("[error]")), | ||
105 | Arc::new(VariantData::new(db, module, v.flavor())?), | ||
106 | )) | ||
107 | }) | ||
108 | .collect::<Cancelable<_>>()? | ||
109 | } else { | ||
110 | Vec::new() | ||
111 | }; | ||
112 | Ok(EnumData { name, variants }) | ||
91 | } | 113 | } |
92 | } | 114 | } |
93 | 115 | ||
@@ -107,6 +129,39 @@ pub enum VariantData { | |||
107 | } | 129 | } |
108 | 130 | ||
109 | impl VariantData { | 131 | impl VariantData { |
132 | pub fn new(db: &impl HirDatabase, module: &Module, flavor: StructFlavor) -> Cancelable<Self> { | ||
133 | Ok(match flavor { | ||
134 | StructFlavor::Tuple(fl) => { | ||
135 | let fields = fl | ||
136 | .fields() | ||
137 | .enumerate() | ||
138 | .map(|(i, fd)| { | ||
139 | Ok(StructField { | ||
140 | name: SmolStr::new(i.to_string()), | ||
141 | ty: Ty::new_opt(db, &module, fd.type_ref())?, | ||
142 | }) | ||
143 | }) | ||
144 | .collect::<Cancelable<_>>()?; | ||
145 | VariantData::Tuple(fields) | ||
146 | } | ||
147 | StructFlavor::Named(fl) => { | ||
148 | let fields = fl | ||
149 | .fields() | ||
150 | .map(|fd| { | ||
151 | Ok(StructField { | ||
152 | name: fd | ||
153 | .name() | ||
154 | .map(|n| n.text()) | ||
155 | .unwrap_or_else(|| SmolStr::new("[error]")), | ||
156 | ty: Ty::new_opt(db, &module, fd.type_ref())?, | ||
157 | }) | ||
158 | }) | ||
159 | .collect::<Cancelable<_>>()?; | ||
160 | VariantData::Struct(fields) | ||
161 | } | ||
162 | StructFlavor::Unit => VariantData::Unit, | ||
163 | }) | ||
164 | } | ||
110 | pub fn fields(&self) -> &[StructField] { | 165 | pub fn fields(&self) -> &[StructField] { |
111 | match *self { | 166 | match *self { |
112 | VariantData::Struct(ref fields) | VariantData::Tuple(ref fields) => fields, | 167 | VariantData::Struct(ref fields) | VariantData::Tuple(ref fields) => fields, |