aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_def
diff options
context:
space:
mode:
authorcynecx <[email protected]>2021-04-18 18:56:13 +0100
committercynecx <[email protected]>2021-04-18 18:56:13 +0100
commit6ed2fd233b569d01169fc888f30c358dd289d260 (patch)
treed5ff73fef371ac596e7c98db1c39c9dd3112f571 /crates/hir_def
parent3d39e77003c5fe5ed9f8f3ac00a170f3804f8337 (diff)
hir_ty: keep body::Expander in TyLoweringContext
Diffstat (limited to 'crates/hir_def')
-rw-r--r--crates/hir_def/src/body.rs18
-rw-r--r--crates/hir_def/src/type_ref.rs38
2 files changed, 11 insertions, 45 deletions
diff --git a/crates/hir_def/src/body.rs b/crates/hir_def/src/body.rs
index 8a9b936ea..131f424cc 100644
--- a/crates/hir_def/src/body.rs
+++ b/crates/hir_def/src/body.rs
@@ -37,13 +37,15 @@ use crate::{
37 37
38/// A subset of Expander that only deals with cfg attributes. We only need it to 38/// A subset of Expander that only deals with cfg attributes. We only need it to
39/// avoid cyclic queries in crate def map during enum processing. 39/// avoid cyclic queries in crate def map during enum processing.
40#[derive(Debug)]
40pub(crate) struct CfgExpander { 41pub(crate) struct CfgExpander {
41 cfg_options: CfgOptions, 42 cfg_options: CfgOptions,
42 hygiene: Hygiene, 43 hygiene: Hygiene,
43 krate: CrateId, 44 krate: CrateId,
44} 45}
45 46
46pub(crate) struct Expander { 47#[derive(Debug)]
48pub struct Expander {
47 cfg_expander: CfgExpander, 49 cfg_expander: CfgExpander,
48 def_map: Arc<DefMap>, 50 def_map: Arc<DefMap>,
49 current_file_id: HirFileId, 51 current_file_id: HirFileId,
@@ -80,11 +82,7 @@ impl CfgExpander {
80} 82}
81 83
82impl Expander { 84impl Expander {
83 pub(crate) fn new( 85 pub fn new(db: &dyn DefDatabase, current_file_id: HirFileId, module: ModuleId) -> Expander {
84 db: &dyn DefDatabase,
85 current_file_id: HirFileId,
86 module: ModuleId,
87 ) -> Expander {
88 let cfg_expander = CfgExpander::new(db, current_file_id, module.krate); 86 let cfg_expander = CfgExpander::new(db, current_file_id, module.krate);
89 let def_map = module.def_map(db); 87 let def_map = module.def_map(db);
90 let ast_id_map = db.ast_id_map(current_file_id); 88 let ast_id_map = db.ast_id_map(current_file_id);
@@ -98,7 +96,7 @@ impl Expander {
98 } 96 }
99 } 97 }
100 98
101 pub(crate) fn enter_expand<T: ast::AstNode>( 99 pub fn enter_expand<T: ast::AstNode>(
102 &mut self, 100 &mut self,
103 db: &dyn DefDatabase, 101 db: &dyn DefDatabase,
104 macro_call: ast::MacroCall, 102 macro_call: ast::MacroCall,
@@ -170,7 +168,7 @@ impl Expander {
170 Ok(ExpandResult { value: Some((mark, node)), err }) 168 Ok(ExpandResult { value: Some((mark, node)), err })
171 } 169 }
172 170
173 pub(crate) fn exit(&mut self, db: &dyn DefDatabase, mut mark: Mark) { 171 pub fn exit(&mut self, db: &dyn DefDatabase, mut mark: Mark) {
174 self.cfg_expander.hygiene = Hygiene::new(db.upcast(), mark.file_id); 172 self.cfg_expander.hygiene = Hygiene::new(db.upcast(), mark.file_id);
175 self.current_file_id = mark.file_id; 173 self.current_file_id = mark.file_id;
176 self.ast_id_map = mem::take(&mut mark.ast_id_map); 174 self.ast_id_map = mem::take(&mut mark.ast_id_map);
@@ -190,7 +188,7 @@ impl Expander {
190 &self.cfg_expander.cfg_options 188 &self.cfg_expander.cfg_options
191 } 189 }
192 190
193 pub(crate) fn current_file_id(&self) -> HirFileId { 191 pub fn current_file_id(&self) -> HirFileId {
194 self.current_file_id 192 self.current_file_id
195 } 193 }
196 194
@@ -210,7 +208,7 @@ impl Expander {
210} 208}
211 209
212#[derive(Debug)] 210#[derive(Debug)]
213pub(crate) struct Mark { 211pub struct Mark {
214 file_id: HirFileId, 212 file_id: HirFileId,
215 ast_id_map: Arc<AstIdMap>, 213 ast_id_map: Arc<AstIdMap>,
216 bomb: DropBomb, 214 bomb: DropBomb,
diff --git a/crates/hir_def/src/type_ref.rs b/crates/hir_def/src/type_ref.rs
index e18712d24..ea29da5da 100644
--- a/crates/hir_def/src/type_ref.rs
+++ b/crates/hir_def/src/type_ref.rs
@@ -1,15 +1,10 @@
1//! HIR for references to types. Paths in these are not yet resolved. They can 1//! HIR for references to types. Paths in these are not yet resolved. They can
2//! be directly created from an ast::TypeRef, without further queries. 2//! be directly created from an ast::TypeRef, without further queries.
3 3
4use hir_expand::{name::Name, AstId, ExpandResult, InFile}; 4use hir_expand::{name::Name, AstId, InFile};
5use syntax::ast; 5use syntax::ast;
6 6
7use crate::{ 7use crate::{body::LowerCtx, path::Path};
8 body::{Expander, LowerCtx},
9 db::DefDatabase,
10 path::Path,
11 ModuleId,
12};
13 8
14#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] 9#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
15pub enum Mutability { 10pub enum Mutability {
@@ -124,7 +119,7 @@ pub enum TypeBound {
124 119
125impl TypeRef { 120impl TypeRef {
126 /// Converts an `ast::TypeRef` to a `hir::TypeRef`. 121 /// Converts an `ast::TypeRef` to a `hir::TypeRef`.
127 pub(crate) fn from_ast(ctx: &LowerCtx, node: ast::Type) -> Self { 122 pub fn from_ast(ctx: &LowerCtx, node: ast::Type) -> Self {
128 match node { 123 match node {
129 ast::Type::ParenType(inner) => TypeRef::from_ast_opt(&ctx, inner.ty()), 124 ast::Type::ParenType(inner) => TypeRef::from_ast_opt(&ctx, inner.ty()),
130 ast::Type::TupleType(inner) => { 125 ast::Type::TupleType(inner) => {
@@ -303,30 +298,3 @@ impl TypeBound {
303 } 298 }
304 } 299 }
305} 300}
306
307pub fn expand_macro_type(
308 db: &dyn DefDatabase,
309 module_id: ModuleId,
310 macro_type: &TypeRef,
311) -> Option<TypeRef> {
312 let macro_call = match macro_type {
313 TypeRef::Macro(macro_call) => macro_call,
314 _ => panic!("expected TypeRef::Macro"),
315 };
316
317 let file_id = macro_call.file_id;
318 let macro_call = macro_call.to_node(db.upcast());
319
320 let mut expander = Expander::new(db, file_id, module_id);
321 let (file_id, expanded) = match expander.enter_expand::<ast::Type>(db, macro_call.clone()) {
322 Ok(ExpandResult { value: Some((mark, expanded)), .. }) => {
323 let file_id = expander.current_file_id();
324 expander.exit(db, mark);
325 (file_id, expanded)
326 }
327 _ => return None,
328 };
329
330 let ctx = LowerCtx::new(db, file_id);
331 return Some(TypeRef::from_ast(&ctx, expanded));
332}