cairo_lang_sierra_generator/
statements_locations.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
use std::ops::Add;

use cairo_lang_defs::db::DefsGroup;
use cairo_lang_defs::diagnostic_utils::StableLocation;
use cairo_lang_diagnostics::ToOption;
use cairo_lang_filesystem::ids::{FileId, FileLongId, VirtualFile};
use cairo_lang_sierra::program::StatementIdx;
use cairo_lang_syntax::node::{Terminal, TypedSyntaxNode};
use cairo_lang_utils::LookupIntern;
use cairo_lang_utils::unordered_hash_map::UnorderedHashMap;
use itertools::Itertools;

use crate::statements_code_locations::{
    SourceCodeLocation, SourceCodeSpan, SourceFileFullPath, StatementsSourceCodeLocations,
};
use crate::statements_functions::StatementsFunctions;

#[cfg(test)]
#[path = "statements_locations_test.rs"]
mod test;

/// Returns an identifier of the function that contains the given [StableLocation].
/// It is a fully qualified path to the function which contains:
/// - fully qualified path to the file module,
/// - relative path to the function in the file module.
pub fn maybe_containing_function_identifier(
    db: &dyn DefsGroup,
    location: StableLocation,
) -> Option<String> {
    let file_id = location.file_id(db.upcast());
    let absolute_semantic_path_to_file_module = file_module_absolute_identifier(db, file_id)?;

    let relative_semantic_path = function_identifier_relative_to_file_module(db, location);
    if relative_semantic_path.is_empty() {
        // In some cases the stable location maps to a code that is a statement like a function call
        // directly in a file module, e.g. `Self::eq(lhs, rhs)` in `core::traits`. This brings no
        // information about the function it was called from.
        None
    } else {
        Some(absolute_semantic_path_to_file_module.add("::").add(&relative_semantic_path))
    }
}

/// Returns an identifier of the function that contains the given [StableLocation].
/// It is a fully qualified path to the function which contains:
/// - fully qualified path to the file module,
/// - relative path to the function in the file module.
///
/// In case the fully qualified path to the file module cannot be found
/// it is replaced in the fully qualified function path by the file name.
pub fn maybe_containing_function_identifier_for_tests(
    db: &dyn DefsGroup,
    location: StableLocation,
) -> Option<String> {
    let file_id = location.file_id(db.upcast());
    let absolute_semantic_path_to_file_module = file_module_absolute_identifier(db, file_id)
        .unwrap_or_else(|| file_id.file_name(db.upcast()));

    let relative_semantic_path = function_identifier_relative_to_file_module(db, location);
    if relative_semantic_path.is_empty() {
        // In some cases the stable location maps to a code that is a statement like a function call
        // directly in a file module, e.g. `Self::eq(lhs, rhs)` in `core::traits`. This brings no
        // information about the function it was called from. It is especially relevant for corelib
        // tests where the first stable location may map to this kind of code.
        None
    } else {
        Some(absolute_semantic_path_to_file_module.add("::").add(&relative_semantic_path))
    }
}

/// Returns the path (modules and impls) to the function in the file.
/// The path is relative to the file module.
pub fn function_identifier_relative_to_file_module(
    db: &dyn DefsGroup,
    location: StableLocation,
) -> String {
    let syntax_db = db.upcast();
    let mut relative_semantic_path_segments: Vec<String> = vec![];
    let mut syntax_node = location.syntax_node(db);
    let mut statement_located_in_function = false;
    loop {
        // TODO(Gil): Extract this function into a trait of syntax kind to support future
        // function containing items (specifically trait functions).
        match syntax_node.kind(syntax_db) {
            cairo_lang_syntax::node::kind::SyntaxKind::FunctionWithBody => {
                let function_name =
                    cairo_lang_syntax::node::ast::FunctionWithBody::from_syntax_node(
                        syntax_db,
                        syntax_node.clone(),
                    )
                    .declaration(syntax_db)
                    .name(syntax_db)
                    .text(syntax_db);

                if relative_semantic_path_segments.is_empty() {
                    statement_located_in_function = true;
                }

                relative_semantic_path_segments.push(function_name.to_string());
            }
            cairo_lang_syntax::node::kind::SyntaxKind::ItemImpl => {
                let impl_name = cairo_lang_syntax::node::ast::ItemImpl::from_syntax_node(
                    syntax_db,
                    syntax_node.clone(),
                )
                .name(syntax_db)
                .text(syntax_db);
                relative_semantic_path_segments.push(impl_name.to_string());
            }
            cairo_lang_syntax::node::kind::SyntaxKind::ItemModule => {
                let module_name = cairo_lang_syntax::node::ast::ItemModule::from_syntax_node(
                    syntax_db,
                    syntax_node.clone(),
                )
                .name(syntax_db)
                .text(syntax_db);
                relative_semantic_path_segments.push(module_name.to_string());
            }
            _ => {}
        }
        if let Some(parent) = syntax_node.parent() {
            syntax_node = parent;
        } else {
            break;
        }
    }

    // If the statement is not located in a function, and it is located a generated file it is
    // probably located in a code block generated by an inline macro such as `array` or `panic`.
    let file_id = location.file_id(db.upcast());
    if !statement_located_in_function
        && matches!(
            file_id.lookup_intern(db),
            FileLongId::Virtual(VirtualFile { parent: Some(_), .. })
        )
    {
        relative_semantic_path_segments.insert(0, file_id.file_name(db.upcast()));
    }

    relative_semantic_path_segments.into_iter().rev().join("::")
}

/// Returns a location in the user file corresponding to the given [StableLocation].
/// It consists of a full path to the file and a text span in the file.
pub fn maybe_code_location(
    db: &dyn DefsGroup,
    location: StableLocation,
) -> Option<(SourceFileFullPath, SourceCodeSpan)> {
    let location = location.diagnostic_location(db.upcast()).user_location(db.upcast());
    let file_full_path = location.file_id.full_path(db.upcast());
    let position = location.span.position_in_file(db.upcast(), location.file_id)?;
    let source_location = SourceCodeSpan {
        start: SourceCodeLocation { col: position.start.col, line: position.start.line },
        end: SourceCodeLocation { col: position.end.col, line: position.end.line },
    };

    Some((SourceFileFullPath(file_full_path), source_location))
}

/// This function returns a fully qualified path to the file module.
/// `None` should be returned only for compiler tests where files of type `VirtualFile` may be non
/// generated files.
pub fn file_module_absolute_identifier(db: &dyn DefsGroup, mut file_id: FileId) -> Option<String> {
    // `VirtualFile` is a generated file (e.g., by macros like `#[starknet::contract]`)
    // that won't have a matching file module in the db. Instead, we find its non generated parent
    // which is in the same module and have a matching file module in the db.
    while let FileLongId::Virtual(VirtualFile { parent: Some(parent), .. }) =
        file_id.lookup_intern(db)
    {
        file_id = parent;
    }

    let file_modules = db.file_modules(file_id).to_option()?;
    let full_path = file_modules.first().unwrap().full_path(db.upcast());

    Some(full_path)
}

/// The locations in the Cairo source code which caused a statement to be generated.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct StatementsLocations {
    pub locations: UnorderedHashMap<StatementIdx, Vec<StableLocation>>,
}

impl StatementsLocations {
    /// Creates a new [StatementsLocations] object from a list of [`Option<StableLocation>`].
    pub fn from_locations_vec(locations_vec: &[Vec<StableLocation>]) -> Self {
        let mut locations = UnorderedHashMap::default();
        for (idx, stmt_locations) in locations_vec.iter().enumerate() {
            if !stmt_locations.is_empty() {
                locations.insert(StatementIdx(idx), stmt_locations.clone());
            }
        }
        Self { locations }
    }
    /// Builds a map between each Sierra statement index and a string representation of the Cairo
    /// function that it was generated from. It is used for places
    /// without db access such as the profiler.
    // TODO(Gil): Add a db access to the profiler and remove this function.
    pub fn get_statements_functions_map_for_tests(
        &self,
        db: &dyn DefsGroup,
    ) -> UnorderedHashMap<StatementIdx, String> {
        self.locations
            .iter_sorted()
            .filter_map(|(statement_idx, stable_locations)| {
                maybe_containing_function_identifier_for_tests(
                    db,
                    *stable_locations.first().unwrap(),
                )
                .map(|function_identifier| (*statement_idx, function_identifier))
            })
            .collect()
    }

    /// Creates a new [StatementsFunctions] struct using [StatementsLocations] and [DefsGroup].
    pub fn extract_statements_functions(&self, db: &dyn DefsGroup) -> StatementsFunctions {
        StatementsFunctions {
            statements_to_functions_map: self
                .locations
                .iter_sorted()
                .map(|(statement_idx, stable_locations)| {
                    (
                        *statement_idx,
                        stable_locations
                            .iter()
                            .filter_map(|s| maybe_containing_function_identifier(db, *s))
                            .collect(),
                    )
                })
                .collect(),
        }
    }

    /// Creates a new [StatementsSourceCodeLocations] struct using [StatementsLocations] and
    /// [DefsGroup].
    pub fn extract_statements_source_code_locations(
        &self,
        db: &dyn DefsGroup,
    ) -> StatementsSourceCodeLocations {
        StatementsSourceCodeLocations {
            statements_to_code_location_map: self
                .locations
                .iter_sorted()
                .map(|(statement_idx, stable_locations)| {
                    (
                        *statement_idx,
                        stable_locations
                            .iter()
                            .filter_map(|s| maybe_code_location(db, *s))
                            .collect(),
                    )
                })
                .collect(),
        }
    }
}