diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-25 11:25:53 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-25 11:25:53 +0000 |
commit | 04ce89313369a1606b057b3be1368d72b8a5cd63 (patch) | |
tree | d85e49c985b92a9ccf1303a089dccdd4c805db1c /crates/ra_hir/src/adt.rs | |
parent | ae97cd59ff086e7efb6409f14c2f8ae8861596e4 (diff) | |
parent | 64d4f4255880bbfb400551db38b76871276eaca6 (diff) |
Merge #638
638: reduce visibility r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/adt.rs')
-rw-r--r-- | crates/ra_hir/src/adt.rs | 100 |
1 files changed, 40 insertions, 60 deletions
diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index dc936e826..ec6a10353 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs | |||
@@ -79,12 +79,13 @@ impl EnumVariant { | |||
79 | .to_owned(); | 79 | .to_owned(); |
80 | (file_id, var) | 80 | (file_id, var) |
81 | } | 81 | } |
82 | pub(crate) fn variant_data(&self, db: &impl HirDatabase) -> Arc<VariantData> { | ||
83 | db.enum_data(self.parent).variants[self.id] | ||
84 | .variant_data | ||
85 | .clone() | ||
86 | } | ||
82 | } | 87 | } |
83 | 88 | ||
84 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
85 | pub(crate) struct EnumVariantId(RawId); | ||
86 | impl_arena_id!(EnumVariantId); | ||
87 | |||
88 | #[derive(Debug, Clone, PartialEq, Eq)] | 89 | #[derive(Debug, Clone, PartialEq, Eq)] |
89 | pub struct EnumData { | 90 | pub struct EnumData { |
90 | pub(crate) name: Option<Name>, | 91 | pub(crate) name: Option<Name>, |
@@ -94,105 +95,84 @@ pub struct EnumData { | |||
94 | impl EnumData { | 95 | impl EnumData { |
95 | pub(crate) fn enum_data_query(db: &impl HirDatabase, e: Enum) -> Arc<EnumData> { | 96 | pub(crate) fn enum_data_query(db: &impl HirDatabase, e: Enum) -> Arc<EnumData> { |
96 | let (_file_id, enum_def) = e.source(db); | 97 | let (_file_id, enum_def) = e.source(db); |
97 | let mut res = EnumData { | 98 | let name = enum_def.name().map(|n| n.as_name()); |
98 | name: enum_def.name().map(|n| n.as_name()), | 99 | let variants = variants(&*enum_def) |
99 | variants: Arena::default(), | 100 | .map(|var| EnumVariantData { |
100 | }; | ||
101 | for var in variants(&*enum_def) { | ||
102 | let data = EnumVariantData { | ||
103 | name: var.name().map(|it| it.as_name()), | 101 | name: var.name().map(|it| it.as_name()), |
104 | variant_data: Arc::new(VariantData::new(var.flavor())), | 102 | variant_data: Arc::new(VariantData::new(var.flavor())), |
105 | }; | 103 | }) |
106 | res.variants.alloc(data); | 104 | .collect(); |
107 | } | 105 | Arc::new(EnumData { name, variants }) |
108 | |||
109 | Arc::new(res) | ||
110 | } | 106 | } |
111 | } | 107 | } |
112 | 108 | ||
109 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
110 | pub(crate) struct EnumVariantId(RawId); | ||
111 | impl_arena_id!(EnumVariantId); | ||
112 | |||
113 | #[derive(Debug, Clone, PartialEq, Eq)] | 113 | #[derive(Debug, Clone, PartialEq, Eq)] |
114 | pub struct EnumVariantData { | 114 | pub(crate) struct EnumVariantData { |
115 | pub(crate) name: Option<Name>, | 115 | pub(crate) name: Option<Name>, |
116 | pub(crate) variant_data: Arc<VariantData>, | 116 | variant_data: Arc<VariantData>, |
117 | } | 117 | } |
118 | 118 | ||
119 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
120 | pub(crate) struct StructFieldId(RawId); | ||
121 | impl_arena_id!(StructFieldId); | ||
122 | |||
119 | /// A single field of an enum variant or struct | 123 | /// A single field of an enum variant or struct |
120 | #[derive(Debug, Clone, PartialEq, Eq)] | 124 | #[derive(Debug, Clone, PartialEq, Eq)] |
121 | pub struct StructField { | 125 | pub struct StructFieldData { |
122 | pub(crate) name: Name, | 126 | pub(crate) name: Name, |
123 | pub(crate) type_ref: TypeRef, | 127 | pub(crate) type_ref: TypeRef, |
124 | } | 128 | } |
125 | 129 | ||
126 | /// Fields of an enum variant or struct | 130 | /// Fields of an enum variant or struct |
127 | #[derive(Debug, Clone, PartialEq, Eq)] | 131 | #[derive(Debug, Clone, PartialEq, Eq)] |
128 | pub enum VariantData { | 132 | pub(crate) struct VariantData(VariantDataInner); |
129 | Struct(Vec<StructField>), | 133 | |
130 | Tuple(Vec<StructField>), | 134 | #[derive(Debug, Clone, PartialEq, Eq)] |
135 | enum VariantDataInner { | ||
136 | Struct(Arena<StructFieldId, StructFieldData>), | ||
137 | Tuple(Arena<StructFieldId, StructFieldData>), | ||
131 | Unit, | 138 | Unit, |
132 | } | 139 | } |
133 | 140 | ||
134 | impl VariantData { | 141 | impl VariantData { |
135 | pub fn fields(&self) -> &[StructField] { | 142 | pub(crate) fn fields(&self) -> Option<&Arena<StructFieldId, StructFieldData>> { |
136 | match self { | 143 | match &self.0 { |
137 | VariantData::Struct(fields) | VariantData::Tuple(fields) => fields, | 144 | VariantDataInner::Struct(fields) | VariantDataInner::Tuple(fields) => Some(fields), |
138 | _ => &[], | 145 | _ => None, |
139 | } | ||
140 | } | ||
141 | |||
142 | pub fn is_struct(&self) -> bool { | ||
143 | match self { | ||
144 | VariantData::Struct(..) => true, | ||
145 | _ => false, | ||
146 | } | ||
147 | } | ||
148 | |||
149 | pub fn is_tuple(&self) -> bool { | ||
150 | match self { | ||
151 | VariantData::Tuple(..) => true, | ||
152 | _ => false, | ||
153 | } | ||
154 | } | ||
155 | |||
156 | pub fn is_unit(&self) -> bool { | ||
157 | match self { | ||
158 | VariantData::Unit => true, | ||
159 | _ => false, | ||
160 | } | 146 | } |
161 | } | 147 | } |
162 | } | 148 | } |
163 | 149 | ||
164 | impl VariantData { | 150 | impl VariantData { |
165 | fn new(flavor: StructFlavor) -> Self { | 151 | fn new(flavor: StructFlavor) -> Self { |
166 | match flavor { | 152 | let inner = match flavor { |
167 | StructFlavor::Tuple(fl) => { | 153 | StructFlavor::Tuple(fl) => { |
168 | let fields = fl | 154 | let fields = fl |
169 | .fields() | 155 | .fields() |
170 | .enumerate() | 156 | .enumerate() |
171 | .map(|(i, fd)| StructField { | 157 | .map(|(i, fd)| StructFieldData { |
172 | name: Name::tuple_field_name(i), | 158 | name: Name::tuple_field_name(i), |
173 | type_ref: TypeRef::from_ast_opt(fd.type_ref()), | 159 | type_ref: TypeRef::from_ast_opt(fd.type_ref()), |
174 | }) | 160 | }) |
175 | .collect(); | 161 | .collect(); |
176 | VariantData::Tuple(fields) | 162 | VariantDataInner::Tuple(fields) |
177 | } | 163 | } |
178 | StructFlavor::Named(fl) => { | 164 | StructFlavor::Named(fl) => { |
179 | let fields = fl | 165 | let fields = fl |
180 | .fields() | 166 | .fields() |
181 | .map(|fd| StructField { | 167 | .map(|fd| StructFieldData { |
182 | name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing), | 168 | name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing), |
183 | type_ref: TypeRef::from_ast_opt(fd.type_ref()), | 169 | type_ref: TypeRef::from_ast_opt(fd.type_ref()), |
184 | }) | 170 | }) |
185 | .collect(); | 171 | .collect(); |
186 | VariantData::Struct(fields) | 172 | VariantDataInner::Struct(fields) |
187 | } | 173 | } |
188 | StructFlavor::Unit => VariantData::Unit, | 174 | StructFlavor::Unit => VariantDataInner::Unit, |
189 | } | 175 | }; |
190 | } | 176 | VariantData(inner) |
191 | |||
192 | pub(crate) fn get_field_type_ref(&self, field_name: &Name) -> Option<&TypeRef> { | ||
193 | self.fields() | ||
194 | .iter() | ||
195 | .find(|f| f.name == *field_name) | ||
196 | .map(|f| &f.type_ref) | ||
197 | } | 177 | } |
198 | } | 178 | } |