From 85cc3cfec99a7d232384efae010bfbc8224f1351 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sun, 3 Jan 2021 11:47:57 +0100 Subject: Revert "Proper handling $crate and local_inner_macros" --- crates/hir_expand/src/hygiene.rs | 151 +++++++-------------------------------- crates/hir_expand/src/lib.rs | 11 +-- 2 files changed, 32 insertions(+), 130 deletions(-) (limited to 'crates/hir_expand') diff --git a/crates/hir_expand/src/hygiene.rs b/crates/hir_expand/src/hygiene.rs index 6042e15b2..7ab0a5e52 100644 --- a/crates/hir_expand/src/hygiene.rs +++ b/crates/hir_expand/src/hygiene.rs @@ -2,94 +2,30 @@ //! //! Specifically, `ast` + `Hygiene` allows you to create a `Name`. Note that, at //! this moment, this is horribly incomplete and handles only `$crate`. -use std::sync::Arc; - -use arena::{Arena, Idx}; use base_db::CrateId; use either::Either; -use mbe::Origin; -use syntax::{ast, AstNode}; +use syntax::ast; use crate::{ db::AstDatabase, name::{AsName, Name}, - ExpansionInfo, HirFileId, HirFileIdRepr, MacroCallId, MacroDefKind, + HirFileId, HirFileIdRepr, MacroCallId, MacroDefKind, }; #[derive(Clone, Debug)] pub struct Hygiene { - frames: Option>, -} - -impl Hygiene { - pub fn new(db: &dyn AstDatabase, file_id: HirFileId) -> Hygiene { - Hygiene { frames: Some(Arc::new(HygieneFrames::new(db, file_id.clone()))) } - } - - pub fn new_unhygienic() -> Hygiene { - Hygiene { frames: None } - } - - // FIXME: this should just return name - pub fn name_ref_to_name(&self, name_ref: ast::NameRef) -> Either { - if let Some(frames) = &self.frames { - if name_ref.text() == "$crate" { - if let Some(krate) = frames.root_crate(&name_ref) { - return Either::Right(krate); - } - } - } - - Either::Left(name_ref.as_name()) - } - - pub fn local_inner_macros(&self, path: ast::Path) -> Option { - let frames = self.frames.as_ref()?; - - let mut token = path.syntax().first_token()?; - let mut current = frames.first(); - - while let Some((frame, data)) = - current.and_then(|it| Some((it, it.expansion.as_ref()?.map_token_up(&token)?))) - { - let (mapped, origin) = data; - if origin == Origin::Def { - return if frame.local_inner { frame.krate } else { None }; - } - current = Some(&frames.0[frame.call_site?]); - token = mapped.value; - } - None - } -} - -#[derive(Default, Debug)] -struct HygieneFrames(Arena); - -#[derive(Clone, Debug)] -struct HygieneFrame { - expansion: Option, + // This is what `$crate` expands to + def_crate: Option, // Indicate this is a local inner macro local_inner: bool, - krate: Option, - - call_site: Option>, - def_site: Option>, } -impl HygieneFrames { - fn new(db: &dyn AstDatabase, file_id: HirFileId) -> Self { - let mut frames = HygieneFrames::default(); - frames.add(db, file_id); - frames - } - - fn add(&mut self, db: &dyn AstDatabase, file_id: HirFileId) -> Option> { - let (krate, local_inner) = match file_id.0 { +impl Hygiene { + pub fn new(db: &dyn AstDatabase, file_id: HirFileId) -> Hygiene { + let (def_crate, local_inner) = match file_id.0 { HirFileIdRepr::FileId(_) => (None, false), HirFileIdRepr::MacroFile(macro_file) => match macro_file.macro_call_id { - MacroCallId::EagerMacro(_id) => (None, false), MacroCallId::LazyMacro(id) => { let loc = db.lookup_intern_macro(id); match loc.def.kind { @@ -100,68 +36,31 @@ impl HygieneFrames { MacroDefKind::ProcMacro(_) => (None, false), } } + MacroCallId::EagerMacro(_id) => (None, false), }, }; - - let expansion = file_id.expansion_info(db); - let expansion = match expansion { - None => { - return Some(self.0.alloc(HygieneFrame { - expansion: None, - local_inner, - krate, - call_site: None, - def_site: None, - })); - } - Some(it) => it, - }; - - let def_site = expansion.def.clone(); - let call_site = expansion.arg.file_id; - let idx = self.0.alloc(HygieneFrame { - expansion: Some(expansion), - local_inner, - krate, - call_site: None, - def_site: None, - }); - - self.0[idx].call_site = self.add(db, call_site); - self.0[idx].def_site = def_site.and_then(|it| self.add(db, it.file_id)); - - Some(idx) + Hygiene { def_crate, local_inner } } - fn first(&self) -> Option<&HygieneFrame> { - self.0.iter().next().map(|it| it.1) + pub fn new_unhygienic() -> Hygiene { + Hygiene { def_crate: None, local_inner: false } } - fn root_crate(&self, name_ref: &ast::NameRef) -> Option { - let mut token = name_ref.syntax().first_token()?; - let first = self.first()?; - let mut result = first.krate; - let mut current = Some(first); - - while let Some((frame, (mapped, origin))) = - current.and_then(|it| Some((it, it.expansion.as_ref()?.map_token_up(&token)?))) - { - result = frame.krate; - - let site = match origin { - Origin::Def => frame.def_site, - Origin::Call => frame.call_site, - }; - - let site = match site { - None => break, - Some(it) => it, - }; - - current = Some(&self.0[site]); - token = mapped.value; + // FIXME: this should just return name + pub fn name_ref_to_name(&self, name_ref: ast::NameRef) -> Either { + if let Some(def_crate) = self.def_crate { + if name_ref.text() == "$crate" { + return Either::Right(def_crate); + } } + Either::Left(name_ref.as_name()) + } - result + pub fn local_inner_macros(&self) -> Option { + if self.local_inner { + self.def_crate + } else { + None + } } } diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs index 5b6734a5f..3fa1b1d77 100644 --- a/crates/hir_expand/src/lib.rs +++ b/crates/hir_expand/src/lib.rs @@ -340,8 +340,11 @@ impl ExpansionInfo { Some(self.expanded.with_value(token)) } - pub fn map_token_up(&self, token: &SyntaxToken) -> Option<(InFile, Origin)> { - let token_id = self.exp_map.token_by_range(token.text_range())?; + pub fn map_token_up( + &self, + token: InFile<&SyntaxToken>, + ) -> Option<(InFile, Origin)> { + let token_id = self.exp_map.token_by_range(token.value.text_range())?; let (token_id, origin) = self.macro_def.0.map_id_up(token_id); let (token_map, tt) = match origin { @@ -356,7 +359,7 @@ impl ExpansionInfo { ), }; - let range = token_map.range_by_token(token_id)?.by_kind(token.kind())?; + let range = token_map.range_by_token(token_id)?.by_kind(token.value.kind())?; let token = algo::find_covering_element(&tt.value, range + tt.value.text_range().start()) .into_token()?; Some((tt.with_value(token), origin)) @@ -492,7 +495,7 @@ fn ascend_call_token( expansion: &ExpansionInfo, token: InFile, ) -> Option> { - let (mapped, origin) = expansion.map_token_up(&token.value)?; + let (mapped, origin) = expansion.map_token_up(token.as_ref())?; if origin != Origin::Call { return None; } -- cgit v1.2.3