Struct miden_diagnostics::CodeMap
source · pub struct CodeMap { /* private fields */ }
Expand description
CodeMap is a thread-safe structure for recording source code files and their contents for use in diagnostics and parsing/compilation.
The CodeMap maintains a set of SourceFile entries corresponding to the sources added to it, with various auxiliary structures for tracking the FileName under which each source was added, the SourceId assigned to it, and which files on disk have been read into memory and added to it.
The CodeMap is designed to de-duplicate files and avoid reading from disk multiple times for the same Path. It is also designed to live for the entire lifetime of the compilation pipeline, so that at any point, diagnostics may be generated which refer to the original sources.
It is generally advised to allocate the CodeMap in an std::sync::Arc, so that it may be freely passed around and accessed from multiple threads and/or contexts which need it. Internally it uses thread-safe datastructures, so there isn’t any reason to prefer passing it around by reference.
Implementations§
source§impl CodeMap
impl CodeMap
sourcepub fn add(&self, name: impl Into<FileName>, source: String) -> SourceId
pub fn add(&self, name: impl Into<FileName>, source: String) -> SourceId
Add a file to this CodeMap, returning the SourceId assigned to it.
The SourceId acts as a unique identifier for the file and content. However, it is not guaranteed that a FileName always maps to a single SourceId, as multiple threads may attempt to add the same file at the same time, which in some cases may result in a duplicate entry. In general though, they are 1:1.
sourcepub fn add_file<P: AsRef<Path>>(&self, path: P) -> Result<SourceId>
pub fn add_file<P: AsRef<Path>>(&self, path: P) -> Result<SourceId>
Adds a file to the map from the given path
, if not already present.
Returns Ok
if successfully added, or Err
if an error occurred
while reading the file from disk.
sourcepub fn add_child(
&self,
name: impl Into<FileName>,
source: String,
parent: SourceSpan
) -> SourceId
pub fn add_child( &self, name: impl Into<FileName>, source: String, parent: SourceSpan ) -> SourceId
Add a file to the map with the given SourceSpan as a parent.
This is intended for use cases such as a preprocessor which needs to include content from another file directly into the content of its parent, as if they are part of the same logical file. When a SourceSpan spans the region in which the included content occurs, it only gets the content in the original parent file.
NOTE: This always results in a new entry in the map in order to record the lineage of the source content.
sourcepub fn get(&self, file_id: SourceId) -> Result<Arc<SourceFile>, Error>
pub fn get(&self, file_id: SourceId) -> Result<Arc<SourceFile>, Error>
Get the SourceFile corresponding to the given SourceId
sourcepub fn get_with_span(&self, span: SourceSpan) -> Result<Arc<SourceFile>, Error>
pub fn get_with_span(&self, span: SourceSpan) -> Result<Arc<SourceFile>, Error>
Get the SourceFile corresponding to the given SourceSpan
Returns Err
if the span is SourceSpan::UNKNOWN
sourcepub fn parent(&self, file_id: SourceId) -> Option<SourceSpan>
pub fn parent(&self, file_id: SourceId) -> Option<SourceSpan>
Get the SourceSpan corresponding to the parent of a given SourceId.
Returns None
if the given SourceId has no parent
sourcepub fn get_file_id(&self, filename: &FileName) -> Option<SourceId>
pub fn get_file_id(&self, filename: &FileName) -> Option<SourceId>
sourcepub fn get_by_name(&self, filename: &FileName) -> Option<Arc<SourceFile>>
pub fn get_by_name(&self, filename: &FileName) -> Option<Arc<SourceFile>>
Get the SourceFile corresponding to the given FileName
sourcepub fn name_for_spanned<S: Spanned>(
&self,
spanned: &S
) -> Result<FileName, Error>
pub fn name_for_spanned<S: Spanned>( &self, spanned: &S ) -> Result<FileName, Error>
Get the FileName associated with the given SourceSpan
Returns Err
if span
is SourceSpan::UNKNOWN.
sourcepub fn line_column_to_span(
&self,
file_id: SourceId,
line: impl Into<LineIndex>,
column: impl Into<ColumnIndex>
) -> Result<SourceSpan, Error>
pub fn line_column_to_span( &self, file_id: SourceId, line: impl Into<LineIndex>, column: impl Into<ColumnIndex> ) -> Result<SourceSpan, Error>
Get a SourceSpan corresponding to the given line:column
NOTE: The returned SourceSpan points only to line:column, it does not span any neighboring source locations, callers must extend the returned span if so desired.
sourcepub fn location<S: Spanned>(&self, spanned: &S) -> Result<Location, Error>
pub fn location<S: Spanned>(&self, spanned: &S) -> Result<Location, Error>
Get a Location from a SourceSpan
Returns Err
if span
is SourceSpan::UNKNOWN.
sourcepub fn location_at_index(
&self,
file_id: SourceId,
byte_index: impl Into<ByteIndex>
) -> Result<Location, Error>
pub fn location_at_index( &self, file_id: SourceId, byte_index: impl Into<ByteIndex> ) -> Result<Location, Error>
sourcepub fn source_span(&self, file_id: SourceId) -> Result<SourceSpan, Error>
pub fn source_span(&self, file_id: SourceId) -> Result<SourceSpan, Error>
Get a SourceSpan representing the entire content of file_id
Trait Implementations§
source§impl<'a> Files<'a> for CodeMap
impl<'a> Files<'a> for CodeMap
§type FileId = SourceId
type FileId = SourceId
diagnostic::Label
s in the corresponding source files.