use crate::{BytePos, CharPos, Pos, Span};
use solar_data_structures::{
map::FxBuildHasher,
sync::{ReadGuard, RwLock},
};
use std::{
io::{self, Read},
path::{Path, PathBuf},
sync::Arc,
};
mod analyze;
mod file;
pub use file::*;
mod file_resolver;
pub use file_resolver::{FileResolver, ResolveError};
#[cfg(test)]
mod tests;
pub type FileLinesResult = Result<FileLines, SpanLinesError>;
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum SpanLinesError {
DistinctSources(Box<DistinctSources>),
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum SpanSnippetError {
IllFormedSpan(Span),
DistinctSources(Box<DistinctSources>),
MalformedForSourcemap(MalformedSourceMapPositions),
SourceNotAvailable { filename: FileName },
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct DistinctSources {
pub begin: (FileName, BytePos),
pub end: (FileName, BytePos),
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct MalformedSourceMapPositions {
pub name: FileName,
pub source_len: usize,
pub begin_pos: BytePos,
pub end_pos: BytePos,
}
#[derive(Clone, Debug)]
pub struct Loc {
pub file: Arc<SourceFile>,
pub line: usize,
pub col: CharPos,
pub col_display: usize,
}
#[derive(Debug)]
pub struct SourceFileAndLine {
pub sf: Arc<SourceFile>,
pub line: usize,
}
#[derive(Debug)]
pub struct SourceFileAndBytePos {
pub sf: Arc<SourceFile>,
pub pos: BytePos,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct LineInfo {
pub line_index: usize,
pub start_col: CharPos,
pub end_col: CharPos,
}
pub struct FileLines {
pub file: Arc<SourceFile>,
pub lines: Vec<LineInfo>,
}
pub struct SourceMap {
source_files: RwLock<Vec<Arc<SourceFile>>>,
stable_id_to_source_file: scc::HashIndex<StableSourceFileId, Arc<SourceFile>, FxBuildHasher>,
hash_kind: SourceFileHashAlgorithm,
}
impl Default for SourceMap {
fn default() -> Self {
Self::empty()
}
}
impl SourceMap {
pub fn new(hash_kind: SourceFileHashAlgorithm) -> Self {
Self {
source_files: RwLock::new(Vec::new()),
stable_id_to_source_file: Default::default(),
hash_kind,
}
}
pub fn empty() -> Self {
Self::new(SourceFileHashAlgorithm::default())
}
pub fn load_file(&self, path: &Path) -> io::Result<Arc<SourceFile>> {
let filename = path.to_owned().into();
self.new_source_file(filename, || std::fs::read_to_string(path))
}
pub fn load_stdin(&self) -> io::Result<Arc<SourceFile>> {
self.new_source_file(FileName::Stdin, || {
let mut src = String::new();
io::stdin().read_to_string(&mut src)?;
Ok(src)
})
}
pub fn new_dummy_source_file(&self, path: PathBuf, src: String) -> io::Result<Arc<SourceFile>> {
self.new_source_file(path.into(), || Ok(src))
}
#[instrument(level = "debug", skip_all, fields(filename = %filename.display()))]
pub fn new_source_file(
&self,
filename: FileName,
get_src: impl FnOnce() -> io::Result<String>,
) -> io::Result<Arc<SourceFile>> {
let stable_id = StableSourceFileId::from_filename_in_current_crate(&filename);
match self.stable_id_to_source_file.entry(stable_id) {
scc::hash_index::Entry::Occupied(entry) => Ok(entry.get().clone()),
scc::hash_index::Entry::Vacant(entry) => {
let mut file = SourceFile::new(filename, get_src()?, self.hash_kind)?;
debug_assert_eq!(file.stable_id, stable_id);
trace!(name=%file.name.display(), len=file.src.len(), loc=file.count_lines(), "adding to source map");
let mut source_files = self.source_files.write();
file.start_pos = BytePos(if let Some(last_file) = source_files.last() {
last_file.end_position().0.checked_add(1).ok_or(OffsetOverflowError(()))?
} else {
0
});
let file = Arc::new(file);
source_files.push(file.clone());
entry.insert_entry(file.clone());
Ok(file)
}
}
}
pub fn files(&self) -> ReadGuard<'_, Vec<Arc<SourceFile>>> {
self.source_files.read()
}
pub fn source_file_by_file_name(&self, filename: &FileName) -> Option<Arc<SourceFile>> {
let stable_id = StableSourceFileId::from_filename_in_current_crate(filename);
self.source_file_by_stable_id(stable_id)
}
pub fn source_file_by_stable_id(
&self,
stable_id: StableSourceFileId,
) -> Option<Arc<SourceFile>> {
self.stable_id_to_source_file.get(&stable_id).as_deref().cloned()
}
pub fn filename_for_diagnostics<'a>(&self, filename: &'a FileName) -> FileNameDisplay<'a> {
filename.display()
}
pub fn is_multiline(&self, span: Span) -> bool {
let lo = self.lookup_source_file_idx(span.lo());
let hi = self.lookup_source_file_idx(span.hi());
if lo != hi {
return true;
}
let f = self.files()[lo].clone();
let lo = f.relative_position(span.lo());
let hi = f.relative_position(span.hi());
f.lookup_line(lo) != f.lookup_line(hi)
}
pub fn span_to_snippet(&self, span: Span) -> Result<String, SpanSnippetError> {
self.span_to_source(span, |src, start_index, end_index| {
src.get(start_index..end_index)
.map(|s| s.to_string())
.ok_or(SpanSnippetError::IllFormedSpan(span))
})
}
pub fn lookup_byte_offset(&self, bpos: BytePos) -> SourceFileAndBytePos {
let idx = self.lookup_source_file_idx(bpos);
let sf = self.files()[idx].clone();
let offset = bpos - sf.start_pos;
SourceFileAndBytePos { sf, pos: offset }
}
pub fn lookup_source_file_idx(&self, pos: BytePos) -> usize {
self.files().partition_point(|x| x.start_pos <= pos) - 1
}
pub fn lookup_source_file(&self, pos: BytePos) -> Arc<SourceFile> {
let idx = self.lookup_source_file_idx(pos);
self.files()[idx].clone()
}
pub fn lookup_char_pos(&self, pos: BytePos) -> Loc {
let sf = self.lookup_source_file(pos);
let (line, col, col_display) = sf.lookup_file_pos_with_col_display(pos);
Loc { file: sf, line, col, col_display }
}
pub fn lookup_line(&self, pos: BytePos) -> Result<SourceFileAndLine, Arc<SourceFile>> {
let f = self.lookup_source_file(pos);
let pos = f.relative_position(pos);
match f.lookup_line(pos) {
Some(line) => Ok(SourceFileAndLine { sf: f, line }),
None => Err(f),
}
}
pub fn span_to_prev_source(&self, sp: Span) -> Result<String, SpanSnippetError> {
self.span_to_source(sp, |src, start_index, _| {
src.get(..start_index).map(|s| s.to_string()).ok_or(SpanSnippetError::IllFormedSpan(sp))
})
}
pub fn is_valid_span(&self, sp: Span) -> Result<(Loc, Loc), SpanLinesError> {
let lo = self.lookup_char_pos(sp.lo());
let hi = self.lookup_char_pos(sp.hi());
if lo.file.start_pos != hi.file.start_pos {
return Err(SpanLinesError::DistinctSources(Box::new(DistinctSources {
begin: (lo.file.name.clone(), lo.file.start_pos),
end: (hi.file.name.clone(), hi.file.start_pos),
})));
}
Ok((lo, hi))
}
pub fn is_line_before_span_empty(&self, sp: Span) -> bool {
match self.span_to_prev_source(sp) {
Ok(s) => s.rsplit_once('\n').unwrap_or(("", &s)).1.trim_start().is_empty(),
Err(_) => false,
}
}
pub fn span_to_lines(&self, sp: Span) -> FileLinesResult {
let (lo, hi) = self.is_valid_span(sp)?;
assert!(hi.line >= lo.line);
if sp.is_dummy() {
return Ok(FileLines { file: lo.file, lines: Vec::new() });
}
let mut lines = Vec::with_capacity(hi.line - lo.line + 1);
let mut start_col = lo.col;
let hi_line = hi.line.saturating_sub(1);
for line_index in lo.line.saturating_sub(1)..hi_line {
let line_len = lo.file.get_line(line_index).map_or(0, |s| s.chars().count());
lines.push(LineInfo { line_index, start_col, end_col: CharPos::from_usize(line_len) });
start_col = CharPos::from_usize(0);
}
lines.push(LineInfo { line_index: hi_line, start_col, end_col: hi.col });
Ok(FileLines { file: lo.file, lines })
}
fn span_to_source<F, T>(&self, sp: Span, extract_source: F) -> Result<T, SpanSnippetError>
where
F: Fn(&str, usize, usize) -> Result<T, SpanSnippetError>,
{
let local_begin = self.lookup_byte_offset(sp.lo());
let local_end = self.lookup_byte_offset(sp.hi());
if local_begin.sf.start_pos != local_end.sf.start_pos {
Err(SpanSnippetError::DistinctSources(Box::new(DistinctSources {
begin: (local_begin.sf.name.clone(), local_begin.sf.start_pos),
end: (local_end.sf.name.clone(), local_end.sf.start_pos),
})))
} else {
let start_index = local_begin.pos.to_usize();
let end_index = local_end.pos.to_usize();
let source_len = local_begin.sf.source_len.to_usize();
if start_index > end_index || end_index > source_len {
return Err(SpanSnippetError::MalformedForSourcemap(MalformedSourceMapPositions {
name: local_begin.sf.name.clone(),
source_len,
begin_pos: local_begin.pos,
end_pos: local_end.pos,
}));
}
extract_source(&local_begin.sf.src, start_index, end_index)
}
}
pub fn span_to_diagnostic_string(&self, sp: Span) -> String {
self.span_to_string(sp)
}
pub fn span_to_string(&self, sp: Span) -> String {
let (source_file, lo_line, lo_col, hi_line, hi_col) = self.span_to_location_info(sp);
let file_name = match source_file {
Some(sf) => sf.name.display().to_string(),
None => return "no-location".to_string(),
};
format!("{file_name}:{lo_line}:{lo_col}: {hi_line}:{hi_col}")
}
pub fn span_to_location_info(
&self,
sp: Span,
) -> (Option<Arc<SourceFile>>, usize, usize, usize, usize) {
if self.files().is_empty() || sp.is_dummy() {
return (None, 0, 0, 0, 0);
}
let lo = self.lookup_char_pos(sp.lo());
let hi = self.lookup_char_pos(sp.hi());
(Some(lo.file), lo.line, lo.col.to_usize() + 1, hi.line, hi.col.to_usize() + 1)
}
}