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`.
|
2019-10-30 11:10:53 -05:00
|
|
|
use ra_db::CrateId;
|
|
|
|
use ra_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,
|
|
|
|
either::Either,
|
|
|
|
name::{AsName, Name},
|
2019-11-11 04:45:55 -06:00
|
|
|
HirFileId, HirFileIdRepr, MacroDefKind,
|
2019-10-30 10:56:20 -05:00
|
|
|
};
|
2019-10-30 10:41:50 -05:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Hygiene {
|
|
|
|
// This is what `$crate` expands to
|
|
|
|
def_crate: Option<CrateId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Hygiene {
|
|
|
|
pub fn new(db: &impl AstDatabase, file_id: HirFileId) -> Hygiene {
|
2019-10-30 11:10:53 -05:00
|
|
|
let def_crate = match file_id.0 {
|
|
|
|
HirFileIdRepr::FileId(_) => None,
|
|
|
|
HirFileIdRepr::MacroFile(macro_file) => {
|
|
|
|
let loc = db.lookup_intern_macro(macro_file.macro_call_id);
|
2019-11-11 04:45:55 -06:00
|
|
|
match loc.def.kind {
|
|
|
|
MacroDefKind::Declarative => Some(loc.def.krate),
|
|
|
|
MacroDefKind::BuiltIn(_) => None,
|
2019-11-09 21:03:24 -06:00
|
|
|
}
|
2019-10-30 11:10:53 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
Hygiene { def_crate }
|
2019-10-30 10:41:50 -05:00
|
|
|
}
|
|
|
|
|
2019-10-30 11:10:53 -05:00
|
|
|
pub fn new_unhygienic() -> Hygiene {
|
2019-10-30 10:41:50 -05:00
|
|
|
Hygiene { def_crate: None }
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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" {
|
|
|
|
return Either::B(def_crate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Either::A(name_ref.as_name())
|
|
|
|
}
|
|
|
|
}
|