2019-10-30 10:41:50 -05: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`.
|
2020-08-13 09:25:38 -05:00
|
|
|
use base_db::CrateId;
|
2019-12-03 10:07:56 -06:00
|
|
|
use either::Either;
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::ast;
|
2019-10-30 10:41:50 -05:00
|
|
|
|
2019-10-30 11:10:53 -05:00
|
|
|
use crate::{
|
2019-10-30 10:56:20 -05:00
|
|
|
db::AstDatabase,
|
|
|
|
name::{AsName, Name},
|
2020-03-02 00:05:15 -06:00
|
|
|
HirFileId, HirFileIdRepr, MacroCallId, MacroDefKind,
|
2019-10-30 10:56:20 -05:00
|
|
|
};
|
2019-10-30 10:41:50 -05:00
|
|
|
|
2020-04-30 05:20:13 -05:00
|
|
|
#[derive(Clone, Debug)]
|
2019-10-30 10:41:50 -05:00
|
|
|
pub struct Hygiene {
|
|
|
|
// This is what `$crate` expands to
|
|
|
|
def_crate: Option<CrateId>,
|
2020-04-30 22:23:03 -05:00
|
|
|
|
2020-08-11 03:50:45 -05:00
|
|
|
// Indicate this is a local inner macro
|
2020-04-30 22:23:03 -05:00
|
|
|
local_inner: bool,
|
2019-10-30 10:41:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Hygiene {
|
2020-03-13 10:05:46 -05:00
|
|
|
pub fn new(db: &dyn AstDatabase, file_id: HirFileId) -> Hygiene {
|
2020-04-30 22:23:03 -05:00
|
|
|
let (def_crate, local_inner) = match file_id.0 {
|
|
|
|
HirFileIdRepr::FileId(_) => (None, false),
|
2020-03-02 00:05:15 -06:00
|
|
|
HirFileIdRepr::MacroFile(macro_file) => match macro_file.macro_call_id {
|
|
|
|
MacroCallId::LazyMacro(id) => {
|
|
|
|
let loc = db.lookup_intern_macro(id);
|
|
|
|
match loc.def.kind {
|
2020-04-30 22:23:03 -05:00
|
|
|
MacroDefKind::Declarative => (loc.def.krate, loc.def.local_inner),
|
|
|
|
MacroDefKind::BuiltIn(_) => (None, false),
|
|
|
|
MacroDefKind::BuiltInDerive(_) => (None, false),
|
|
|
|
MacroDefKind::BuiltInEager(_) => (None, false),
|
2020-09-18 08:37:31 -05:00
|
|
|
MacroDefKind::ProcMacro(_) => (None, false),
|
2020-03-02 00:05:15 -06:00
|
|
|
}
|
2019-11-09 21:03:24 -06:00
|
|
|
}
|
2020-04-30 22:23:03 -05:00
|
|
|
MacroCallId::EagerMacro(_id) => (None, false),
|
2020-03-02 00:05:15 -06:00
|
|
|
},
|
2019-10-30 11:10:53 -05:00
|
|
|
};
|
2020-04-30 22:23:03 -05:00
|
|
|
Hygiene { def_crate, local_inner }
|
2019-10-30 10:41:50 -05:00
|
|
|
}
|
|
|
|
|
2019-10-30 11:10:53 -05:00
|
|
|
pub fn new_unhygienic() -> Hygiene {
|
2020-04-30 22:23:03 -05:00
|
|
|
Hygiene { def_crate: None, local_inner: false }
|
2019-10-30 10:41:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: this should just return name
|
2019-10-30 11:10:53 -05:00
|
|
|
pub fn name_ref_to_name(&self, name_ref: ast::NameRef) -> Either<Name, CrateId> {
|
2019-10-30 10:41:50 -05:00
|
|
|
if let Some(def_crate) = self.def_crate {
|
|
|
|
if name_ref.text() == "$crate" {
|
2019-12-03 10:07:56 -06:00
|
|
|
return Either::Right(def_crate);
|
2019-10-30 10:41:50 -05:00
|
|
|
}
|
|
|
|
}
|
2019-12-03 10:07:56 -06:00
|
|
|
Either::Left(name_ref.as_name())
|
2019-10-30 10:41:50 -05:00
|
|
|
}
|
2020-04-30 22:23:03 -05:00
|
|
|
|
|
|
|
pub fn local_inner_macros(&self) -> Option<CrateId> {
|
|
|
|
if self.local_inner {
|
|
|
|
self.def_crate
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2019-10-30 10:41:50 -05:00
|
|
|
}
|