tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

75 lignes · 2924 octetsBrut · Historique
1//! The std modules a program reaches (ADR 0074): only they are resolved,
2//! type checked and lowered. They are found from the parse trees, before
3//! resolution, so a module that is not reached costs nothing past its
4//! (shared) parse.
5
6use std::collections::HashMap;
7
8use louise_syntax::ast::{self, AstNode};
9use louise_syntax::{Parse, SyntaxKind};
10
11use crate::{Module, PRELUDE};
12
13/// Which of `modules` to analyse, by index: every module outside std,
14/// the prelude, and what they reach, transitively, through their imports
15/// (`@test import`s only with `tests`) and the syntax that names a std
16/// module itself ([`language_modules`]).
17pub(crate) fn reached(modules: &[Module], tests: bool) -> Vec<bool> {
18 let index: HashMap<&str, usize> = (modules.iter().enumerate())
19 .map(|(i, m)| (m.path.as_str(), i))
20 .collect();
21 let mut reached = vec![false; modules.len()];
22 let mut work: Vec<usize> = (0..modules.len())
23 .filter(|&i| !modules[i].path.starts_with("std/"))
24 .collect();
25 work.extend(index.get(PRELUDE));
26 while let Some(i) = work.pop() {
27 if std::mem::replace(&mut reached[i], true) {
28 continue;
29 }
30 let m = &modules[i];
31 for import in louise_hir::imports(&m.parse.tree(), tests) {
32 let target =
33 louise_hir::import_target(&m.path, &import.path, |p| index.contains_key(p));
34 work.extend(target.and_then(|t| index.get(t.as_str())));
35 }
36 work.extend(language_modules(&m.parse).filter_map(|p| index.get(p)));
37 }
38 reached
39}
40
41/// The std modules the language names itself, for syntax that needs one
42/// without an import (`louise_types`' `WellKnown`):
43/// - an HTML literal is an `Element` of `std/html`;
44/// - a template literal is a `Template` of `std/template`;
45/// - `region` handles `std/ref`'s `st` and `alloc`;
46/// - a reference to a `@remote` function is a `Remote` of
47/// `std/dist/remote`.
48///
49/// Every other well-known std type or effect is named in the program
50/// only through an import of its module, or in a value that a reached
51/// module made.
52fn language_modules(parse: &Parse) -> impl Iterator<Item = &'static str> {
53 const MODULES: [&str; 4] = ["std/html", "std/template", "std/ref", "std/dist/remote"];
54 let mut found = [false; 4];
55 for node in parse.syntax().descendants() {
56 let which = match node.kind() {
57 SyntaxKind::HTML_ELEMENT => 0,
58 SyntaxKind::TEMPLATE_EXPR => 1,
59 SyntaxKind::REGION_EXPR => 2,
60 SyntaxKind::ATTR
61 if ast::Attr::cast(node)
62 .and_then(|a| a.name())
63 .is_some_and(|n| n.text() == "remote") =>
64 {
65 3
66 }
67 _ => continue,
68 };
69 found[which] = true;
70 }
71 MODULES
72 .into_iter()
73 .zip(found)
74 .filter_map(|(m, f)| f.then_some(m))
75}