From cf3b4f1e208247c9d171273dabff9c6b3c98a240 Mon Sep 17 00:00:00 2001 From: cynecx Date: Sat, 10 Apr 2021 17:49:12 +0200 Subject: hir_ty: Expand macros at type position --- crates/hir_def/src/body/lower.rs | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) (limited to 'crates/hir_def/src/body') diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index c0b0b7841..c11da30d2 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs @@ -1,10 +1,11 @@ //! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr` //! representation. -use std::mem; +use std::{mem, sync::Arc}; use either::Either; use hir_expand::{ + ast_id_map::{AstIdMap, FileAstId}, hygiene::Hygiene, name::{name, AsName, Name}, ExpandError, HirFileId, @@ -39,20 +40,39 @@ use crate::{ use super::{diagnostics::BodyDiagnostic, ExprSource, PatSource}; -pub(crate) struct LowerCtx { +pub struct LowerCtx { hygiene: Hygiene, + file_id: Option, + source_ast_id_map: Option>, } impl LowerCtx { - pub(crate) fn new(db: &dyn DefDatabase, file_id: HirFileId) -> Self { - LowerCtx { hygiene: Hygiene::new(db.upcast(), file_id) } + pub fn new(db: &dyn DefDatabase, file_id: HirFileId) -> Self { + LowerCtx { + hygiene: Hygiene::new(db.upcast(), file_id), + file_id: Some(file_id), + source_ast_id_map: Some(db.ast_id_map(file_id)), + } + } + + pub fn with_hygiene(hygiene: &Hygiene) -> Self { + LowerCtx { hygiene: hygiene.clone(), file_id: None, source_ast_id_map: None } + } + + pub(crate) fn hygiene(&self) -> &Hygiene { + &self.hygiene } - pub(crate) fn with_hygiene(hygiene: &Hygiene) -> Self { - LowerCtx { hygiene: hygiene.clone() } + + pub(crate) fn file_id(&self) -> HirFileId { + self.file_id.unwrap() } pub(crate) fn lower_path(&self, ast: ast::Path) -> Option { - Path::from_src(ast, &self.hygiene) + Path::from_src(ast, self) + } + + pub(crate) fn ast_id(&self, item: &N) -> Option> { + self.source_ast_id_map.as_ref().map(|ast_id_map| ast_id_map.ast_id(item)) } } -- cgit v1.2.3