2023-04-17 10:31:39 -05:00
|
|
|
//! 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};
|
2023-04-17 10:31:39 -05:00
|
|
|
use syntax::ast;
|
2023-05-02 09:12:22 -05:00
|
|
|
use triomphe::Arc;
|
2023-04-17 10:31:39 -05:00
|
|
|
|
|
|
|
use crate::{db::DefDatabase, path::Path};
|
|
|
|
|
|
|
|
pub struct LowerCtx<'a> {
|
|
|
|
pub db: &'a dyn DefDatabase,
|
2024-03-07 07:14:59 -06:00
|
|
|
file_id: HirFileId,
|
|
|
|
span_map: OnceCell<SpanMap>,
|
|
|
|
ast_id_map: OnceCell<Arc<AstIdMap>>,
|
2023-04-17 10:31:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> LowerCtx<'a> {
|
2024-03-07 07:14:59 -06:00
|
|
|
pub fn new(db: &'a dyn DefDatabase, file_id: HirFileId) -> Self {
|
|
|
|
LowerCtx { db, file_id, span_map: OnceCell::new(), ast_id_map: OnceCell::new() }
|
2023-04-17 10:31:39 -05:00
|
|
|
}
|
|
|
|
|
2024-03-07 07:14:59 -06:00
|
|
|
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-04-17 10:31:39 -05:00
|
|
|
}
|
|
|
|
|
2023-11-24 09:38:48 -06:00
|
|
|
pub(crate) fn span_map(&self) -> SpanMapRef<'_> {
|
2024-03-07 07:14:59 -06:00
|
|
|
self.span_map.get_or_init(|| self.db.span_map(self.file_id)).as_ref()
|
2023-04-17 10:31:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn lower_path(&self, ast: ast::Path) -> Option<Path> {
|
2023-11-25 10:10:18 -06:00
|
|
|
Path::from_src(self, ast)
|
2023-04-17 10:31:39 -05:00
|
|
|
}
|
|
|
|
|
2024-03-07 07:14:59 -06:00
|
|
|
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),
|
|
|
|
)
|
2023-04-17 10:31:39 -05:00
|
|
|
}
|
|
|
|
}
|