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

49 lines
1.3 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::{
2023-12-18 06:30:41 -06:00
span_map::{SpanMap, SpanMapRef},
2023-11-24 09:38:48 -06:00
AstId, HirFileId, InFile,
2023-07-04 02:16:15 -05:00
};
2024-03-01 08:39:44 -06:00
use span::{AstIdMap, AstIdNode};
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,
file_id: HirFileId,
span_map: OnceCell<SpanMap>,
ast_id_map: OnceCell<Arc<AstIdMap>>,
}
impl<'a> LowerCtx<'a> {
pub fn new(db: &'a dyn DefDatabase, file_id: HirFileId) -> Self {
LowerCtx { db, file_id, span_map: OnceCell::new(), ast_id_map: OnceCell::new() }
}
pub fn with_span_map_cell(
db: &'a dyn DefDatabase,
file_id: HirFileId,
span_map: OnceCell<SpanMap>,
) -> Self {
LowerCtx { db, file_id, span_map, ast_id_map: OnceCell::new() }
}
2023-11-24 09:38:48 -06:00
pub(crate) fn span_map(&self) -> SpanMapRef<'_> {
self.span_map.get_or_init(|| self.db.span_map(self.file_id)).as_ref()
}
pub(crate) fn lower_path(&self, ast: ast::Path) -> Option<Path> {
2023-11-25 10:10:18 -06:00
Path::from_src(self, ast)
}
pub(crate) fn ast_id<N: AstIdNode>(&self, item: &N) -> AstId<N> {
InFile::new(
self.file_id,
self.ast_id_map.get_or_init(|| self.db.ast_id_map(self.file_id)).ast_id(item),
)
}
}