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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//  LOCATION.rs
//    by Lut99
//
//  Created:
//    05 Sep 2022, 16:27:08
//  Last edited:
//    08 Dec 2023, 17:16:27
//  Auto updated?
//    Yes
//
//  Description:
//!   Resolves the extra location restrictions that on-structures impose.
//!
//!   Note that this traversal is actually only here in a deprecated fashion.
//

use std::collections::HashSet;

use brane_dsl::TextRange;
use brane_dsl::ast::{Attribute, Block, Expr, Literal, Node, Program, Stmt};
use brane_dsl::location::{AllowedLocations, Location};
use enum_debug::EnumDebug as _;

use crate::errors::AstError;
pub use crate::errors::LocationError as Error;


/***** TESTS *****/
#[cfg(test)]
mod tests {
    use brane_dsl::ParserOptions;
    use brane_shr::utilities::{create_data_index, create_package_index, test_on_dsl_files};
    use specifications::data::DataIndex;
    use specifications::package::PackageIndex;

    use super::super::print::dsl;
    use super::*;
    use crate::{CompileResult, CompileStage, compile_program_to};


    /// Tests the traversal by generating symbol tables for every file.
    #[test]
    fn test_location() {
        test_on_dsl_files("BraneScript", |path, code| {
            // Start by the name to always know which file this is
            println!("{}", (0..80).map(|_| '-').collect::<String>());
            println!("File '{}' gave us:", path.display());

            // Load the package index
            let pindex: PackageIndex = create_package_index();
            let dindex: DataIndex = create_data_index();

            // Run up to this traversal
            let program: Program = match compile_program_to(code.as_bytes(), &pindex, &dindex, &ParserOptions::bscript(), CompileStage::Location) {
                CompileResult::Program(p, warns) => {
                    // Print warnings if any
                    for w in warns {
                        w.prettyprint(path.to_string_lossy(), &code);
                    }
                    p
                },
                CompileResult::Eof(err) => {
                    // Print the error
                    err.prettyprint(path.to_string_lossy(), &code);
                    panic!("Failed to analyse locations (see output above)");
                },
                CompileResult::Err(errs) => {
                    // Print the errors
                    for e in errs {
                        e.prettyprint(path.to_string_lossy(), &code);
                    }
                    panic!("Failed to analyse locations (see output above)");
                },

                _ => {
                    unreachable!();
                },
            };

            // Now print the file for prettyness
            dsl::do_traversal(program, std::io::stdout()).unwrap();
            println!("{}\n\n", (0..80).map(|_| '-').collect::<String>());
        });
    }
}





/***** HELPER FUNCTIONS *****/
/// Searches the given attributes for `loc`/`location`-attributes and use that to scope the given [`AllowedLocations`].
///
/// # Arguments
/// - `attrs`: The list of attributes to search.
/// - `locations`: The [`AllowedLocations`] list to scope down.
/// - `reasons`: A trail of [`TextRange`]s that is used to point to all attributes leading to the current (faultive) scope.
/// - `errors`: A list used to keep track of occurred errors.
///
/// # Errors
/// This function may error if the current combination of attributes leads to zero possible locations.
fn process_attrs_loc_location(attrs: &[Attribute], locations: &mut AllowedLocations, reasons: &mut Vec<TextRange>, errors: &mut Vec<Error>) {
    for attr in attrs {
        match attr {
            Attribute::List { key, values, range } => {
                if key.value == "on" || key.value == "loc" || key.value == "location" {
                    // Keep track of where this lives for errors
                    reasons.push(range.clone());

                    // Assert the values a literals
                    let locs: HashSet<Location> = values
                        .iter()
                        .filter_map(|value| {
                            if let Literal::String { value, range: _ } = value {
                                Some(Location::from(value.clone()))
                            } else {
                                errors.push(Error::IllegalLocation { range: value.range().clone() });
                                None
                            }
                        })
                        .collect();

                    // Compute the intersection with the existing one
                    locations.intersection(&mut AllowedLocations::Exclusive(locs));
                    if locations.is_empty() {
                        errors.push(Error::OnNoLocation { range: range.clone(), reasons: reasons.clone() });
                        return;
                    }
                }
            },

            // Ignore other attributes
            Attribute::KeyPair { .. } => {},
        }
    }
}





/***** TRAVERSAL FUNCTIONS *****/
/// Attempts to resolve the location restrictions of all function calls in this Stmt.
///
/// # Arguments
/// - `stmt`: The Stmt to traverse.
/// - `locations`: The current restriction of locations as imposed by the on-structs.
/// - `reasons`: The ranges of the on-structs that somehow restrict the current call.
/// - `errors`: A list we use to accumulate errors as they occur.
///
/// # Errors
/// This function may error if there were semantic problems while resolving the locations.
///
/// If errors occur, they are appended to the `errors` list. The function is early-quit in that case.
fn pass_stmt(stmt: &mut Stmt, mut locations: AllowedLocations, mut reasons: Vec<TextRange>, errors: &mut Vec<Error>) {
    // Match on the exact statement
    use Stmt::*;
    #[allow(clippy::collapsible_match)]
    match stmt {
        Block { block } => {
            pass_block(block, locations, reasons, errors);
        },

        FuncDef { ident: _, params: _, code, st_entry: _, attrs, range: _ } => {
            process_attrs_loc_location(attrs, &mut locations, &mut reasons, errors);
            pass_block(code, locations, reasons, errors);
        },
        ClassDef { ident: _, props: _, methods, st_entry: _, symbol_table: _, attrs, range: _ } => {
            // Analyse the attributes for location scopes
            process_attrs_loc_location(attrs, &mut locations, &mut reasons, errors);

            // Apply to method bodies
            for m in methods {
                pass_stmt(m, locations.clone(), reasons.clone(), errors);
            }
        },
        Return { expr, data_type: _, output: _, attrs, range: _ } => {
            if let Some(expr) = expr {
                process_attrs_loc_location(attrs, &mut locations, &mut reasons, errors);
                pass_expr(expr, locations, reasons, errors);
            }
        },

        If { cond, consequent, alternative, attrs, range: _ } => {
            // Apply attributes
            process_attrs_loc_location(attrs, &mut locations, &mut reasons, errors);

            // Pass everything in this statement
            pass_expr(cond, locations.clone(), reasons.clone(), errors);
            pass_block(consequent, locations.clone(), reasons.clone(), errors);
            if let Some(alternative) = alternative {
                pass_block(alternative, locations, reasons, errors)
            };
        },
        For { initializer, condition, increment, consequent, attrs, range: _ } => {
            // Apply attributes
            process_attrs_loc_location(attrs, &mut locations, &mut reasons, errors);

            // Pass everything in this statement
            pass_stmt(initializer, locations.clone(), reasons.clone(), errors);
            pass_expr(condition, locations.clone(), reasons.clone(), errors);
            pass_stmt(increment, locations.clone(), reasons.clone(), errors);
            pass_block(consequent, locations, reasons, errors);
        },
        While { condition, consequent, attrs, range: _ } => {
            // Apply attributes
            process_attrs_loc_location(attrs, &mut locations, &mut reasons, errors);

            // Pass everything in this statement
            pass_expr(condition, locations.clone(), reasons.clone(), errors);
            pass_block(consequent, locations, reasons, errors);
        },
        Parallel { blocks, merge: _, result: _, st_entry: _, attrs, range: _ } => {
            // Apply attributes
            process_attrs_loc_location(attrs, &mut locations, &mut reasons, errors);

            // Pass everything in this statement
            for b in blocks {
                pass_block(b, locations.clone(), reasons.clone(), errors);
            }
        },

        LetAssign { value, name: _, st_entry: _, attrs, range: _ } => {
            process_attrs_loc_location(attrs, &mut locations, &mut reasons, errors);
            pass_expr(value, locations, reasons, errors);
        },
        Assign { name: _, value, st_entry: _, attrs, range: _ } => {
            process_attrs_loc_location(attrs, &mut locations, &mut reasons, errors);
            pass_expr(value, locations, reasons, errors);
        },
        Expr { expr, data_type: _, attrs, range: _ } => {
            process_attrs_loc_location(attrs, &mut locations, &mut reasons, errors);
            pass_expr(expr, locations, reasons, errors);
        },

        // The rest no matter
        Import { .. } | Empty { .. } => {},
        Attribute(_) | AttributeInner(_) => panic!("Encountered {:?} in location traversal", stmt.variant()),
    };
}

/// Attempts to resolve the location restrictions of all function calls in this Block.
///
/// # Arguments
/// - `block`: The Block to traverse.
/// - `locations`: The current restriction of locations as imposed by the on-structs.
/// - `reasons`: The ranges of the on-structs that somehow restrict the current call.
/// - `errors`: A list we use to accumulate errors as they occur.
///
/// # Errors
/// This function may error if there were semantic problems while resolving the locations.
///
/// If errors occur, they are appended to the `errors` list. The function is early-quit in that case.
fn pass_block(block: &mut Block, mut locations: AllowedLocations, mut reasons: Vec<TextRange>, errors: &mut Vec<Error>) {
    // Inspect if the block has annotations about the location
    process_attrs_loc_location(&block.attrs, &mut locations, &mut reasons, errors);

    // Then recurse into the statements with the location restrictions
    for s in &mut block.stmts {
        pass_stmt(s, locations.clone(), reasons.clone(), errors);
    }
}

/// Attempts to resolve the location restrictions of all function calls in this Expr.
///
/// # Arguments
/// - `expr`: The Expr to traverse.
/// - `on_locations`: The current restriction of locations as imposed by the on-structs.
/// - `on_reasons`: The ranges of the on-structs that somehow restrict the current call.
/// - `errors`: A list we use to accumulate errors as they occur.
///
/// # Returns
/// This function returns the restrictions of the expression as a whole, together with a list of sources for that restriction. This only applies to calls within it, but is necessary for parent calls to know about.
///
/// # Errors
/// This function may error if there were semantic problems while resolving the locations.
///
/// If errors occur, they are appended to the `errors` list. The function is early-quit in that case.
fn pass_expr(expr: &mut Expr, on_locations: AllowedLocations, on_reasons: Vec<TextRange>, errors: &mut Vec<Error>) {
    use Expr::*;
    match expr {
        Cast { expr, .. } => {
            pass_expr(expr, on_locations, on_reasons, errors);
        },

        Call { expr, args, ref mut locations, range, .. } => {
            // Resolve the nested stuff first
            pass_expr(expr, on_locations.clone(), on_reasons.clone(), errors);
            for a in args {
                pass_expr(a, on_locations.clone(), on_reasons.clone(), errors);
            }

            // Add the current location if it added to the restriction
            let mut on_reasons: Vec<TextRange> = on_reasons;
            if locations.is_exclusive() {
                on_reasons.push(range.clone());
            }

            // Take the union of the already imposed restrictions + those imposed by On-blocks
            let mut on_locations: AllowedLocations = on_locations;
            locations.intersection(&mut on_locations);
            if locations.is_empty() {
                errors.push(Error::NoLocation { range: range.clone(), reasons: on_reasons });
            }
        },
        Array { values, .. } => {
            for v in values {
                pass_expr(v, on_locations.clone(), on_reasons.clone(), errors);
            }
        },
        ArrayIndex { array, index, .. } => {
            pass_expr(array, on_locations.clone(), on_reasons.clone(), errors);
            pass_expr(index, on_locations, on_reasons, errors);
        },

        UnaOp { expr, .. } => {
            pass_expr(expr, on_locations, on_reasons, errors);
        },
        BinOp { lhs, rhs, .. } => {
            pass_expr(lhs, on_locations.clone(), on_reasons.clone(), errors);
            pass_expr(rhs, on_locations, on_reasons, errors);
        },
        Proj { lhs, rhs, .. } => {
            pass_expr(lhs, on_locations.clone(), on_reasons.clone(), errors);
            pass_expr(rhs, on_locations, on_reasons, errors);
        },

        Instance { properties, .. } => {
            for p in properties {
                pass_expr(&mut p.value, on_locations.clone(), on_reasons.clone(), errors);
            }
        },

        // The rest we don't care
        _ => {},
    }
}





/***** LIBRARY *****/
/// Resolves typing in the given `brane-dsl` AST.
///
/// Note that the symbol tables must already have been constructed.
///
/// This effectively resolves all unresolved types in the symbol tables and verifies everything is compatible. Additionally, it may also insert implicit type casts where able.
///
/// # Arguments
/// - `root`: The root node of the tree on which this compiler pass will be done.
///
/// # Returns
/// The same nodes as went in, but now with no unresolved types.
///
/// # Errors
/// This pass may throw multiple `AstError::ResolveError`s if the user made mistakes with their variable references.
pub fn do_traversal(root: Program) -> Result<Program, Vec<AstError>> {
    let mut root = root;

    // Iterate over all statements to build their symbol tables (if relevant)
    let mut errors: Vec<Error> = vec![];
    pass_block(&mut root.block, AllowedLocations::All, vec![], &mut errors);

    // Done
    if errors.is_empty() { Ok(root) } else { Err(errors.into_iter().map(|e| e.into()).collect()) }
}