2019-10-30 15:41:50 +00:00
|
|
|
//! 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`.
|
2021-01-02 12:25:05 +00:00
|
|
|
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;
|
2019-12-03 16:07:56 +00:00
|
|
|
use either::Either;
|
2021-01-02 12:25:05 +00:00
|
|
|
use mbe::Origin;
|
|
|
|
use syntax::{ast, AstNode};
|
2019-10-30 15:41:50 +00:00
|
|
|
|
2019-10-30 16:10:53 +00:00
|
|
|
use crate::{
|
2019-10-30 15:56:20 +00:00
|
|
|
db::AstDatabase,
|
|
|
|
name::{AsName, Name},
|
2021-01-02 12:25:05 +00:00
|
|
|
ExpansionInfo, HirFileId, HirFileIdRepr, MacroCallId, MacroDefKind,
|
2019-10-30 15:56:20 +00:00
|
|
|
};
|
2019-10-30 15:41:50 +00:00
|
|
|
|
2020-04-30 10:20:13 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2019-10-30 15:41:50 +00:00
|
|
|
pub struct Hygiene {
|
2021-01-02 12:25:05 +00:00
|
|
|
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();
|
2021-01-02 12:25:05 +00:00
|
|
|
|
|
|
|
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?]);
|
2021-01-02 12:25:05 +00:00
|
|
|
token = mapped.value;
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-02 14:48:22 +00:00
|
|
|
#[derive(Default, Debug)]
|
|
|
|
struct HygieneFrames(Arena<HygieneFrame>);
|
2021-01-02 12:25:05 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
struct HygieneFrame {
|
|
|
|
expansion: Option<ExpansionInfo>,
|
2020-05-01 03:23:03 +00:00
|
|
|
|
2020-08-11 08:50:45 +00:00
|
|
|
// Indicate this is a local inner macro
|
2020-05-01 03:23:03 +00:00
|
|
|
local_inner: bool,
|
2021-01-02 12:25:05 +00:00
|
|
|
krate: Option<CrateId>,
|
|
|
|
|
2021-01-02 14:48:22 +00:00
|
|
|
call_site: Option<Idx<HygieneFrame>>,
|
|
|
|
def_site: Option<Idx<HygieneFrame>>,
|
2019-10-30 15:41:50 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 12:25:05 +00:00
|
|
|
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>> {
|
2021-01-02 12:25:05 +00:00
|
|
|
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 {
|
2021-01-02 12:25:05 +00:00
|
|
|
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 {
|
2020-12-15 19:33:05 +00:00
|
|
|
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),
|
2020-09-18 13:37:31 +00:00
|
|
|
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
|
|
|
};
|
2019-10-30 15:41:50 +00:00
|
|
|
|
2021-01-02 12:25:05 +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 {
|
2021-01-02 12:25:05 +00:00
|
|
|
expansion: None,
|
|
|
|
local_inner,
|
|
|
|
krate,
|
|
|
|
call_site: None,
|
|
|
|
def_site: None,
|
2021-01-02 14:48:22 +00:00
|
|
|
}));
|
2021-01-02 12:25:05 +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 {
|
2021-01-02 12:25:05 +00:00
|
|
|
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)
|
2019-10-30 15:41:50 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 14:48:22 +00:00
|
|
|
fn first(&self) -> Option<&HygieneFrame> {
|
|
|
|
self.0.iter().next().map(|it| it.1)
|
2019-10-30 15:41:50 +00:00
|
|
|
}
|
2020-05-01 03:23:03 +00:00
|
|
|
|
2021-01-02 12:25:05 +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()?;
|
2021-01-02 12:25:05 +00:00
|
|
|
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]);
|
2021-01-02 12:25:05 +00:00
|
|
|
token = mapped.value;
|
2020-05-01 03:23:03 +00:00
|
|
|
}
|
2021-01-02 12:25:05 +00:00
|
|
|
|
|
|
|
result
|
2020-05-01 03:23:03 +00:00
|
|
|
}
|
2019-10-30 15:41:50 +00:00
|
|
|
}
|