Skip to main content

cairo_lang_sierra_generator/debug_info/
mod.rs

1use cairo_lang_defs::diagnostic_utils::StableLocation;
2use cairo_lang_filesystem::ids::FileLongId;
3use salsa::Database;
4use serde::{Deserialize, Serialize};
5
6mod function_debug_info;
7mod statements_locations;
8mod type_names;
9
10pub use function_debug_info::serializable::{
11    SerializableAllFunctionsDebugInfo, SerializableFunctionDebugInfo,
12};
13pub use function_debug_info::{AllFunctionsDebugInfo, FunctionDebugInfo};
14pub use statements_locations::StatementsLocations;
15pub use statements_locations::statements_code_locations::StatementsSourceCodeLocations;
16pub use statements_locations::statements_functions::StatementsFunctions;
17pub use type_names::{EnumInfo, SerializableTypeNamesDebugInfo, SierraTypeId, StructInfo};
18
19#[derive(Clone, Debug, Eq, PartialEq)]
20pub struct SierraProgramDebugInfo<'db> {
21    pub statements_locations: StatementsLocations<'db>,
22    pub functions_info: AllFunctionsDebugInfo<'db>,
23}
24
25/// A full path to a Cairo source file.
26#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
27pub struct SourceFileFullPath(pub String);
28
29/// A location in a Cairo source file.
30#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
31pub struct SourceCodeLocation {
32    /// Line index, 0 based.
33    pub line: usize,
34    /// Character index inside the line, 0 based.
35    pub col: usize,
36}
37
38/// A location in a Cairo source file.
39#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
40pub struct SourceCodeSpan {
41    /// Beginning of the text span in the Cairo source file.
42    pub start: SourceCodeLocation,
43    /// End of the text span in the Cairo source file, not included.
44    pub end: SourceCodeLocation,
45}
46
47impl SourceCodeSpan {
48    pub fn contains(&self, other: &Self) -> bool {
49        self.start <= other.start && self.end >= other.end
50    }
51}
52
53/// Returns a location in the user file corresponding to the given [StableLocation].
54/// It consists of a full path to the file, a text span in the file and a boolean indicating
55/// if the location is a part of a macro expansion.
56fn maybe_code_location<'db>(
57    db: &'db dyn Database,
58    location: StableLocation<'db>,
59) -> Option<(SourceFileFullPath, SourceCodeSpan, bool)> {
60    let is_macro =
61        matches!(location.file_id(db).long(db), FileLongId::Virtual(_) | FileLongId::External(_));
62    let location = location.span_in_file(db).user_location(db);
63    let file_full_path = location.file_id.full_path(db);
64    let position = location.span.position_in_file(db, location.file_id)?;
65    let source_location = SourceCodeSpan {
66        start: SourceCodeLocation { col: position.start.col, line: position.start.line },
67        end: SourceCodeLocation { col: position.end.col, line: position.end.line },
68    };
69
70    Some((SourceFileFullPath(file_full_path), source_location, is_macro))
71}