rust-analyzer/crates/hir_expand/src/hygiene.rs

168 lines
5 KiB
Rust
Raw Normal View History

//! This modules handles hygiene information.
//!
//! 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;
2021-01-02 14:48:22 +00:00
use arena::{Arena, Idx};
2020-08-13 14:25:38 +00:00
use base_db::CrateId;
use either::Either;
use mbe::Origin;
use syntax::{ast, AstNode};
2019-10-30 16:10:53 +00:00
use crate::{
2019-10-30 15:56:20 +00:00
db::AstDatabase,
name::{AsName, Name},
ExpansionInfo, HirFileId, HirFileIdRepr, MacroCallId, MacroDefKind,
2019-10-30 15:56:20 +00:00
};
2020-04-30 10:20:13 +00:00
#[derive(Clone, Debug)]
pub struct Hygiene {
frames: Option<Arc<HygieneFrames>>,
}
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<Name, CrateId> {
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<CrateId> {
let frames = self.frames.as_ref()?;
let mut token = path.syntax().first_token()?;
2021-01-02 14:48:22 +00:00
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 };
}
2021-01-02 14:48:22 +00:00
current = Some(&frames.0[frame.call_site?]);
token = mapped.value;
}
None
}
}
2021-01-02 14:48:22 +00:00
#[derive(Default, Debug)]
struct HygieneFrames(Arena<HygieneFrame>);
#[derive(Clone, Debug)]
struct HygieneFrame {
expansion: Option<ExpansionInfo>,
2020-05-01 03:23:03 +00:00
// Indicate this is a local inner macro
2020-05-01 03:23:03 +00:00
local_inner: bool,
krate: Option<CrateId>,
2021-01-02 14:48:22 +00:00
call_site: Option<Idx<HygieneFrame>>,
def_site: Option<Idx<HygieneFrame>>,
}
impl HygieneFrames {
fn new(db: &dyn AstDatabase, file_id: HirFileId) -> Self {
let mut frames = HygieneFrames::default();
frames.add(db, file_id);
frames
}
2021-01-02 14:48:22 +00:00
fn add(&mut self, db: &dyn AstDatabase, file_id: HirFileId) -> Option<Idx<HygieneFrame>> {
let (krate, local_inner) = match file_id.0 {
2020-05-01 03:23:03 +00:00
HirFileIdRepr::FileId(_) => (None, false),
2020-03-02 06:05:15 +00:00
HirFileIdRepr::MacroFile(macro_file) => match macro_file.macro_call_id {
MacroCallId::EagerMacro(_id) => (None, false),
2020-03-02 06:05:15 +00:00
MacroCallId::LazyMacro(id) => {
let loc = db.lookup_intern_macro(id);
match loc.def.kind {
MacroDefKind::Declarative => (Some(loc.def.krate), loc.def.local_inner),
MacroDefKind::BuiltIn(_) => (Some(loc.def.krate), false),
2020-05-01 03:23:03 +00:00
MacroDefKind::BuiltInDerive(_) => (None, false),
MacroDefKind::BuiltInEager(_) => (None, false),
MacroDefKind::ProcMacro(_) => (None, false),
2020-03-02 06:05:15 +00:00
}
2019-11-10 03:03:24 +00:00
}
2020-03-02 06:05:15 +00:00
},
2019-10-30 16:10:53 +00:00
};
let expansion = file_id.expansion_info(db);
let expansion = match expansion {
None => {
2021-01-02 14:48:22 +00:00
return Some(self.0.alloc(HygieneFrame {
expansion: None,
local_inner,
krate,
call_site: None,
def_site: None,
2021-01-02 14:48:22 +00:00
}));
}
Some(it) => it,
};
let def_site = expansion.def.clone();
let call_site = expansion.arg.file_id;
2021-01-02 14:48:22 +00:00
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));
2021-01-02 14:48:22 +00:00
Some(idx)
}
2021-01-02 14:48:22 +00:00
fn first(&self) -> Option<&HygieneFrame> {
self.0.iter().next().map(|it| it.1)
}
2020-05-01 03:23:03 +00:00
fn root_crate(&self, name_ref: &ast::NameRef) -> Option<CrateId> {
let mut token = name_ref.syntax().first_token()?;
2021-01-02 14:48:22 +00:00
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,
};
2021-01-02 14:48:22 +00:00
current = Some(&self.0[site]);
token = mapped.value;
2020-05-01 03:23:03 +00:00
}
result
2020-05-01 03:23:03 +00:00
}
}