rust/crates/hir-def/src/lower.rs

48 lines
1.5 KiB
Rust
Raw Normal View History

//! Context for lowering paths.
2023-09-01 10:30:59 -05:00
use std::cell::OnceCell;
2023-07-04 02:16:15 -05:00
use hir_expand::{
ast_id_map::{AstIdMap, AstIdNode},
2023-11-24 09:38:48 -06:00
span::{SpanMap, SpanMapRef},
AstId, HirFileId, InFile,
2023-07-04 02:16:15 -05:00
};
use syntax::ast;
2023-05-02 09:12:22 -05:00
use triomphe::Arc;
use crate::{db::DefDatabase, path::Path};
pub struct LowerCtx<'a> {
pub db: &'a dyn DefDatabase,
2023-11-24 09:38:48 -06:00
hygiene: SpanMap,
2023-11-17 12:07:31 -06:00
// FIXME: This optimization is probably pointless, ast id map should pretty much always exist anyways.
ast_id_map: Option<(HirFileId, OnceCell<Arc<AstIdMap>>)>,
}
impl<'a> LowerCtx<'a> {
2023-11-24 09:38:48 -06:00
pub fn new(db: &'a dyn DefDatabase, hygiene: SpanMap, file_id: HirFileId) -> Self {
2023-11-17 12:07:31 -06:00
LowerCtx { db, hygiene, ast_id_map: Some((file_id, OnceCell::new())) }
}
pub fn with_file_id(db: &'a dyn DefDatabase, file_id: HirFileId) -> Self {
2023-11-17 12:07:31 -06:00
LowerCtx { db, hygiene: db.span_map(file_id), ast_id_map: Some((file_id, OnceCell::new())) }
}
2023-11-24 09:38:48 -06:00
pub fn with_hygiene(db: &'a dyn DefDatabase, hygiene: SpanMap) -> Self {
2023-11-17 12:07:31 -06:00
LowerCtx { db, hygiene, ast_id_map: None }
}
2023-11-24 09:38:48 -06:00
pub(crate) fn span_map(&self) -> SpanMapRef<'_> {
self.hygiene.as_ref()
}
pub(crate) fn lower_path(&self, ast: ast::Path) -> Option<Path> {
Path::from_src(ast, self)
}
2023-07-04 02:16:15 -05:00
pub(crate) fn ast_id<N: AstIdNode>(&self, item: &N) -> Option<AstId<N>> {
let &(file_id, ref ast_id_map) = self.ast_id_map.as_ref()?;
let ast_id_map = ast_id_map.get_or_init(|| self.db.ast_id_map(file_id));
Some(InFile::new(file_id, ast_id_map.ast_id(item)))
}
}