Skip to main content

ridl_core/
package.rs

1//! The salsa package model (ADR-0002 §1).
2//!
3//! A **package is a directory** (ADR-0002 §1): a named set of [`InputFile`]s
4//! loaded as one unit, carrying its own manifest's `[imports]`. A
5//! [`Workspace`] holds every loaded [`Package`] plus the workspace root's
6//! `[imports]`, kept separate so the ADR-0002 §5 resolution order stays
7//! expressible per member. Both are salsa inputs — the filesystem loader
8//! ([`load_workspace`](crate::workspace::load_workspace), behind the `fs`
9//! feature) builds them; queries downstream read them through the database,
10//! so an edit to one file invalidates only that file's parse.
11//!
12//! This module touches no filesystem: it defines the inputs, the
13//! [`package_of`] lookup query, and the pure package-declaration reader the
14//! loader's law checks build on.
15
16use std::collections::{BTreeMap, HashMap, HashSet};
17
18use ridl_syntax::ast::{AstNode as _, PathType, ServiceDef, SourceFile};
19use ridl_syntax::{SyntaxKind, SyntaxNode};
20use rowan::TextRange;
21
22use crate::db::InputFile;
23use crate::diag::{DiagCode, Diagnostic, Label, Severity, SourceMap, Span};
24use crate::interface_lock::InterfaceLock;
25use crate::manifest::TimingDefaults;
26
27/// Where a package's sources come from.
28#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
29pub enum PackageOrigin {
30    /// Loaded from the local filesystem: a workspace member, a standalone
31    /// package, or a single-file synthetic package.
32    WorkspaceMember,
33    /// Fetched from a remote URL.
34    Remote,
35    /// The embedded `ridl.std` (ADR-0007 decision 15).
36    Std,
37}
38
39/// The package's `interfaces.lock` as the loader read it (lock design §2): the
40/// file's path and text, kept so a checker diagnostic can point into the file
41/// (RIDL-409 is reported on an entry's line), and the parsed table.
42#[derive(Debug, Clone, PartialEq, Eq, Hash)]
43pub struct PackageLock {
44    /// The file's path, in the loader's path form; its parent directory is
45    /// the package directory `ridl lock` names.
46    pub path: String,
47    /// The file's text, byte for byte.
48    pub text: String,
49    /// The parsed table.
50    pub lock: InterfaceLock,
51}
52
53/// One loaded package: its dotted name (e.g. `veh.common`), its files, where
54/// it came from, its own manifest's `[imports]` map, and its interface lock.
55///
56/// `imports` is ADR-0002 §5 step 2: the `[imports]` of the manifest governing
57/// this package's directory tree. It shadows the workspace `[imports]` for
58/// this package only — a member's pin is never visible to a sibling member.
59/// Every package loaded from one manifest's tree (the root and its
60/// subdirectory packages) carries that manifest's map; a single-file package
61/// and `ridl.std` carry an empty map.
62#[salsa::input(debug)]
63pub struct Package {
64    pub name: String,
65    #[returns(ref)]
66    pub files: Vec<InputFile>,
67    pub origin: PackageOrigin,
68    #[returns(ref)]
69    pub imports: BTreeMap<String, String>,
70    /// The raw `[defaults]` timing strings that govern this package: `timing`,
71    /// `command_timing` and `query_timing` (ADR-0002 §5 precedence: package
72    /// `[defaults]` shadows workspace `[defaults]`, merged per key at load).
73    /// A key no manifest sets is `None`; the checker then applies the built-in
74    /// default (ridl §9.1). Stored unparsed: `ridl-core` cannot depend on
75    /// `ridl-sem`.
76    #[returns(ref)]
77    pub defaults: TimingDefaults,
78    /// The package's `interfaces.lock`, read by the loader from the package
79    /// directory (lock design §2), or `None` when the directory has no such
80    /// file, when the file is malformed (the loader reports RIDL-410 and
81    /// drops it), or when the package was not loaded from a directory (a
82    /// source string, `ridl.std`). The checker reads it to give every
83    /// interface its number; without it every number is provisional.
84    #[returns(ref)]
85    pub lock: Option<PackageLock>,
86}
87
88/// Every loaded package plus the workspace root's own `[imports]` map.
89///
90/// `imports` is ADR-0002 §5 step 3 only — the shared default for every
91/// member. It is **not** merged with member maps: the task 9 resolver walks
92/// the order itself — workspace member ([`package_of`]) → the referencing
93/// package's own [`Package::imports`] → this map → error. In a standalone
94/// package load the manifest's `[imports]` ride on the packages and this map
95/// is empty.
96#[salsa::input(debug)]
97pub struct Workspace {
98    #[returns(ref)]
99    pub packages: Vec<Package>,
100    #[returns(ref)]
101    pub imports: BTreeMap<String, String>,
102}
103
104/// The package named `name` in `ws`, or `None` when no loaded package carries
105/// that name. The embedded `ridl.std` is not part of `ws` — the resolver
106/// reaches it through [`std_package`](crate::std_lib::std_package).
107#[salsa::tracked(returns(clone))]
108pub fn package_of(db: &dyn salsa::Database, ws: Workspace, name: String) -> Option<Package> {
109    ws.packages(db)
110        .iter()
111        .copied()
112        .find(|package| package.name(db) == &name)
113}
114
115/// Every `package` declaration in a parsed file, in source order, as the
116/// dotted name plus the declaration's source range. The loader's law checks
117/// read this: the first entry names the file's package, every later entry is a
118/// TYPL-001, and a first entry that does not match the directory's expected
119/// name is a TYPL-002.
120///
121/// The only caller outside the tests is the `fs`-gated loader, so the
122/// `--no-default-features` build allows the helper to be unused.
123#[cfg_attr(not(feature = "fs"), allow(dead_code))]
124pub(crate) fn package_declarations(file: &SourceFile) -> Vec<(String, TextRange)> {
125    file.syntax()
126        .children()
127        .filter_map(ridl_syntax::ast::PackageDecl::cast)
128        .map(|decl| {
129            let name = decl
130                .qualified_name()
131                .map(|name| dotted_name(&name))
132                .unwrap_or_default();
133            (name, decl.syntax().text_range())
134        })
135        .collect()
136}
137
138/// The dotted text of a qualified name, with any trivia between its tokens
139/// dropped (`veh . common` reads as `veh.common`).
140#[cfg_attr(not(feature = "fs"), allow(dead_code))]
141fn dotted_name(name: &ridl_syntax::ast::QualifiedName) -> String {
142    name.syntax()
143        .children_with_tokens()
144        .filter_map(|element| element.into_token())
145        .filter(|token| !token.kind().is_trivia())
146        .map(|token| token.text().to_string())
147        .collect()
148}
149
150// ==========================================================================
151// The service catalog (ridl reference §14.5)
152// ==========================================================================
153
154/// One entry of the [`ServiceCatalog`]: the package that declared a service
155/// and the interfaces it names.
156///
157/// `interface_refs` holds the canonical interface references of the shape
158/// list, in slot order (ADR-0015 decision 12) — each a bare `Name` for a
159/// same-package or unresolved shape, a fully qualified `pkg.Name` when the
160/// name resolves through the declaring package's imports. Service-level
161/// `reserved` tombstones hold a slot but name no interface, so they do not
162/// appear here; a service with an inline shape carries an empty list
163/// (ridl §14.5). `inline` tells the inline shape from a list that names no
164/// interface, one holding only tombstones: rsdl's `requires` accepts the first
165/// and not the second (rsdl reference §3.2).
166#[derive(Debug, Clone, PartialEq, Eq)]
167pub struct CatalogEntry {
168    pub package: String,
169    pub interface_refs: Vec<String>,
170    pub inline: bool,
171}
172
173/// The system-wide service catalog: every `service` declaration across the
174/// workspace keyed by its dotted global name (ridl §14.5).
175///
176/// The catalog is the flat global namespace the whole system agrees on. A
177/// dotted name declared twice anywhere in the workspace is a RIDL-140 error
178/// (both declarations labeled). The diagnostics carry a [`FileId`] indexing
179/// the workspace's files in package-then-file order — the order
180/// [`service_catalog`] interns them — so a driver remaps them onto its render
181/// [`SourceMap`] exactly as it does the package-scoped passes.
182#[derive(Debug, Clone, Default, PartialEq, Eq)]
183pub struct ServiceCatalog {
184    pub entries: BTreeMap<String, CatalogEntry>,
185    pub diagnostics: Vec<Diagnostic>,
186}
187
188/// Builds the workspace-wide [`ServiceCatalog`] and reports the RIDL-140
189/// duplicate-name diagnostics (ridl §14.5, §16.4).
190///
191/// The `std` package is threaded in for signature parity with the
192/// package-scoped passes (`resolve_package`, `check_package`); `ridl.std`
193/// declares no services, so it contributes nothing here. The catalog owns the
194/// global namespace: a service's dotted name is deliberately not a
195/// [`SymbolKind`](../../ridl_sem/resolve/enum.SymbolKind.html) — it lives in
196/// this namespace, not the type namespace — so the resolver never sees it and
197/// the catalog is the sole authority on service uniqueness.
198#[salsa::tracked(returns(clone))]
199pub fn service_catalog(db: &dyn salsa::Database, ws: Workspace, std: Package) -> ServiceCatalog {
200    // `ridl.std` carries no services; the parameter keeps the signature
201    // uniform with the package-scoped passes the callers already drive.
202    let _ = std;
203
204    // Diagnostics point into the workspace's files; their FileId indexes every
205    // file in package-then-file order, the order interned here. A driver
206    // rebuilds the same order to remap onto its render source map.
207    let mut sources = SourceMap::new();
208
209    let mut entries: BTreeMap<String, CatalogEntry> = BTreeMap::new();
210    let mut first_span: HashMap<String, Span> = HashMap::new();
211    let mut diagnostics = Vec::new();
212
213    for package in ws.packages(db) {
214        let package_name = package.name(db).clone();
215        // The package-wide binding view the resolver builds: local
216        // declarations and imports are collected across every file of the
217        // package, and a local declaration shadows an import.
218        let names = package_names(db, package);
219        for file in package.files(db) {
220            let file_id = sources.file_id(file.path(db), file.text(db));
221            let source = service_source(db, *file);
222            for service in source.services() {
223                let Some(dotted) = service.name() else {
224                    continue;
225                };
226                let name = significant_node_text(dotted.syntax());
227                if name.is_empty() {
228                    continue;
229                }
230                let span = Span {
231                    file: file_id,
232                    range: dotted.syntax().text_range(),
233                };
234                if let Some(&first) = first_span.get(&name) {
235                    // RIDL-140: the flat global namespace already holds this
236                    // name. Label both declarations (the first via a secondary
237                    // label, the duplicate as the primary span).
238                    diagnostics.push(Diagnostic {
239                        code: DiagCode::RIDL_140,
240                        severity: Severity::Error,
241                        message: format!(
242                            "duplicate service name `{name}` — the service catalog is a flat global namespace"
243                        ),
244                        primary: span,
245                        labels: vec![Label {
246                            span: first,
247                            message: format!("`{name}` is first declared here"),
248                        }],
249                        fixits: Vec::new(),
250                    });
251                    continue;
252                }
253                first_span.insert(name.clone(), span);
254                entries.insert(
255                    name,
256                    CatalogEntry {
257                        package: package_name.clone(),
258                        interface_refs: canonical_interface_refs(&names, &service),
259                        inline: service.colon_token().is_none(),
260                    },
261                );
262            }
263        }
264    }
265
266    ServiceCatalog {
267        entries,
268        diagnostics,
269    }
270}
271
272/// The parsed [`SourceFile`] of one input, through the memoized parse query.
273fn service_source(db: &dyn salsa::Database, file: InputFile) -> SourceFile {
274    let parse = crate::parse_file(db, file);
275    SourceFile::cast(parse.syntax()).expect("the parser roots every tree in a SourceFile")
276}
277
278/// The canonical interface references a service's list names, in source
279/// order, or the empty list for an inline shape (ADR-0015 decision 12). The
280/// `:` token discriminates the two forms.
281///
282/// A multi-segment reference is already package-qualified and is kept as
283/// written. A single-segment reference resolves against the **package-wide**
284/// binding view ([`package_names`]), in the resolver's precedence: a local
285/// declaration anywhere in the package shadows an import, and an import bound
286/// in any file of the package canonicalizes the name to its full `pkg.Name`
287/// path. An unresolved name stays bare. This is the same bare/qualified split
288/// the checker's IR lowering produces, so the catalog and the IR always agree.
289fn canonical_interface_refs(names: &PackageNames, service: &ServiceDef) -> Vec<String> {
290    if service.colon_token().is_none() {
291        return Vec::new();
292    }
293    service
294        .shapes()
295        .map(|path| canonical_ref(names, &path))
296        .collect()
297}
298
299/// One canonical reference — see [`canonical_interface_refs`] for the rule.
300fn canonical_ref(names: &PackageNames, path: &PathType) -> String {
301    let written = significant_node_text(path.syntax());
302    if written.is_empty() || written.contains('.') {
303        return written;
304    }
305    // A local declaration shadows an import (ridl-sem `apply_imports`), so a
306    // name declared anywhere in this package stays bare.
307    if names.locals.contains(&written) {
308        return written;
309    }
310    if let Some(full) = names.imports.get(&written) {
311        return full.clone();
312    }
313    written
314}
315
316/// The package-wide names a reference can bind to, mirroring the resolver's
317/// precedence: local declarations (any file) shadow imports (any file).
318struct PackageNames {
319    locals: HashSet<String>,
320    imports: HashMap<String, String>,
321}
322
323fn package_names(db: &dyn salsa::Database, package: &Package) -> PackageNames {
324    let mut locals = HashSet::new();
325    let mut imports: HashMap<String, String> = HashMap::new();
326    for file in package.files(db) {
327        let source = service_source(db, *file);
328        for definition in source.definitions() {
329            if let Some(name) = declaration_name(definition.syntax()) {
330                locals.insert(name);
331            }
332        }
333        for interface in source.interfaces() {
334            if let Some(name) = declaration_name(interface.syntax()) {
335                locals.insert(name);
336            }
337        }
338        for import in source.imports() {
339            let Some(qualified) = import.qualified_name() else {
340                continue;
341            };
342            let full = significant_node_text(qualified.syntax());
343            let base = full.rsplit('.').next().unwrap_or(full.as_str()).to_string();
344            let local = import
345                .alias()
346                .and_then(|alias| alias.ident_token())
347                .map(|token| token.text().to_string())
348                .unwrap_or(base);
349            // Among colliding imports the first wins, as in `apply_imports`.
350            imports.entry(local).or_insert(full);
351        }
352    }
353    PackageNames { locals, imports }
354}
355
356/// The identifier of a declaration's `Name` child.
357fn declaration_name(node: &SyntaxNode) -> Option<String> {
358    node.children()
359        .find(|child| child.kind() == SyntaxKind::Name)?
360        .children_with_tokens()
361        .filter_map(|element| element.into_token())
362        .find(|token| token.kind() == SyntaxKind::Ident)
363        .map(|token| token.text().to_string())
364}
365
366/// The concatenation of every non-trivia token in `node`'s subtree — the
367/// node's text with whitespace and comments removed (`veh . adas . cruise`
368/// reads as `veh.adas.cruise`).
369fn significant_node_text(node: &SyntaxNode) -> String {
370    node.descendants_with_tokens()
371        .filter_map(|element| element.into_token())
372        .filter(|token| !token.kind().is_trivia())
373        .map(|token| token.text().to_string())
374        .collect()
375}
376
377#[cfg(test)]
378mod tests {
379    use super::*;
380    use crate::db::RidlDatabase;
381
382    fn file(db: &RidlDatabase, path: &str, text: &str) -> InputFile {
383        InputFile::new(db, path.to_string(), text.to_string())
384    }
385
386    #[test]
387    fn package_of_finds_a_package_by_name() {
388        let db = RidlDatabase::default();
389        let common = Package::new(
390            &db,
391            "veh.common".to_string(),
392            vec![file(&db, "veh-common/a.typl", "package veh.common")],
393            PackageOrigin::WorkspaceMember,
394            BTreeMap::new(),
395            TimingDefaults::default(),
396            None,
397        );
398        let cluster = Package::new(
399            &db,
400            "veh.cluster".to_string(),
401            vec![file(&db, "veh-cluster/b.typl", "package veh.cluster")],
402            PackageOrigin::WorkspaceMember,
403            BTreeMap::new(),
404            TimingDefaults::default(),
405            None,
406        );
407        let ws = Workspace::new(&db, vec![common, cluster], BTreeMap::new());
408
409        assert_eq!(
410            package_of(&db, ws, "veh.cluster".to_string()),
411            Some(cluster)
412        );
413        assert_eq!(package_of(&db, ws, "veh.common".to_string()), Some(common));
414        assert_eq!(package_of(&db, ws, "veh.absent".to_string()), None);
415    }
416
417    #[test]
418    fn package_declarations_reads_names_and_ranges() {
419        let parse = ridl_syntax::parse(
420            "package veh.common\npackage veh.extra\ntype A: m\n",
421            ridl_syntax::Profile::Typl,
422        );
423        let source = SourceFile::cast(parse.syntax()).expect("root is a SourceFile");
424        let decls = package_declarations(&source);
425        assert_eq!(decls.len(), 2, "both declarations are read");
426        assert_eq!(decls[0].0, "veh.common");
427        assert_eq!(decls[0].1, TextRange::new(0.into(), 18.into()));
428        assert_eq!(decls[1].0, "veh.extra");
429        assert_eq!(decls[1].1, TextRange::new(19.into(), 36.into()));
430    }
431
432    // --- the service catalog (ridl reference §14.5) ---------------
433
434    fn ridl_package(db: &RidlDatabase, name: &str, text: &str) -> Package {
435        Package::new(
436            db,
437            name.to_string(),
438            vec![file(db, &format!("{}.ridl", name.replace('.', "/")), text)],
439            PackageOrigin::WorkspaceMember,
440            BTreeMap::new(),
441            TimingDefaults::default(),
442            None,
443        )
444    }
445
446    #[test]
447    fn service_catalog_flags_a_duplicate_name_across_packages() {
448        use crate::std_lib::std_package;
449        let mut db = RidlDatabase::default();
450        let std = std_package(&mut db);
451        // Two packages both declare `veh.adas.cruise` — the flat global
452        // namespace forbids it (RIDL-140).
453        let adas = ridl_package(
454            &db,
455            "veh.adas",
456            "package veh.adas\nservice veh.adas.cruise : Foo\n",
457        );
458        let redundant = ridl_package(
459            &db,
460            "veh.redundant",
461            "package veh.redundant\nservice veh.adas.cruise : Bar\n",
462        );
463        let ws = Workspace::new(&db, vec![adas, redundant], BTreeMap::new());
464
465        let catalog = service_catalog(&db, ws, std);
466
467        assert_eq!(catalog.diagnostics.len(), 1);
468        let diagnostic = &catalog.diagnostics[0];
469        assert_eq!(diagnostic.code, DiagCode::RIDL_140);
470        // Both declarations are labeled: the duplicate as the primary span, the
471        // first via one secondary label.
472        assert_eq!(diagnostic.labels.len(), 1);
473        // The name survives in the catalog once (the first declaration).
474        assert!(catalog.entries.contains_key("veh.adas.cruise"));
475        assert_eq!(catalog.entries["veh.adas.cruise"].package, "veh.adas");
476    }
477
478    #[test]
479    fn service_catalog_resolves_a_cross_package_interface_ref_through_imports() {
480        use crate::std_lib::std_package;
481        let mut db = RidlDatabase::default();
482        let std = std_package(&mut db);
483        let common = ridl_package(
484            &db,
485            "veh.common",
486            "package veh.common\ninterface CruiseControl {\n  signal engaged : boolean\n}\n",
487        );
488        let adas = ridl_package(
489            &db,
490            "veh.adas",
491            "package veh.adas\nimport veh.common.CruiseControl\nservice veh.adas.cruise : CruiseControl\n",
492        );
493        let ws = Workspace::new(&db, vec![common, adas], BTreeMap::new());
494
495        let catalog = service_catalog(&db, ws, std);
496
497        assert!(catalog.diagnostics.is_empty());
498        let entry = &catalog.entries["veh.adas.cruise"];
499        assert_eq!(entry.package, "veh.adas");
500        // The bare `CruiseControl` reference canonicalizes to its full
501        // `pkg.Name` path through the declaring file's import.
502        assert_eq!(entry.interface_refs, ["veh.common.CruiseControl"]);
503    }
504
505    /// A shape list (ADR-0015 decision 12) records every named reference in
506    /// source order, each canonicalized on its own.
507    #[test]
508    fn service_catalog_records_every_shape_of_a_composed_service() {
509        use crate::std_lib::std_package;
510        let mut db = RidlDatabase::default();
511        let std = std_package(&mut db);
512        let common = ridl_package(
513            &db,
514            "veh.common",
515            "package veh.common\ninterface DiagBlock {\n  query readFault(code: boolean): boolean\n}\n",
516        );
517        let body = ridl_package(
518            &db,
519            "veh.body",
520            "package veh.body\nimport veh.common.DiagBlock\n\
521             interface DoorControl {\n  signal locked : boolean\n}\n\
522             service veh.body.doors : DoorControl, DiagBlock\n",
523        );
524        let ws = Workspace::new(&db, vec![common, body], BTreeMap::new());
525
526        let catalog = service_catalog(&db, ws, std);
527
528        assert!(catalog.diagnostics.is_empty());
529        let entry = &catalog.entries["veh.body.doors"];
530        assert_eq!(
531            entry.interface_refs,
532            ["DoorControl", "veh.common.DiagBlock"],
533            "source order, local bare, import canonicalized",
534        );
535        assert!(!entry.inline);
536    }
537
538    #[test]
539    fn service_catalog_records_an_inline_shape_with_an_empty_ref() {
540        use crate::std_lib::std_package;
541        let mut db = RidlDatabase::default();
542        let std = std_package(&mut db);
543        let hvac = ridl_package(
544            &db,
545            "veh.hvac",
546            "package veh.hvac\nservice veh.hvac.cabin {\n  signal temperature : boolean\n}\n",
547        );
548        let ws = Workspace::new(&db, vec![hvac], BTreeMap::new());
549
550        let catalog = service_catalog(&db, ws, std);
551
552        assert!(catalog.diagnostics.is_empty());
553        let entry = &catalog.entries["veh.hvac.cabin"];
554        assert_eq!(entry.package, "veh.hvac");
555        assert_eq!(entry.interface_refs, Vec::<String>::new());
556        assert!(entry.inline);
557    }
558
559    /// A list holding only a tombstone names no interface, as an inline shape
560    /// does; `inline` tells the two forms apart.
561    #[test]
562    fn service_catalog_tells_a_tombstone_only_list_from_an_inline_shape() {
563        use crate::std_lib::std_package;
564        let mut db = RidlDatabase::default();
565        let std = std_package(&mut db);
566        let body = ridl_package(
567            &db,
568            "veh.body",
569            "package veh.body\nservice veh.body.legacy : reserved LegacyDoorDiag\n",
570        );
571        let ws = Workspace::new(&db, vec![body], BTreeMap::new());
572
573        let catalog = service_catalog(&db, ws, std);
574
575        let entry = &catalog.entries["veh.body.legacy"];
576        assert_eq!(entry.interface_refs, Vec::<String>::new());
577        assert!(!entry.inline);
578    }
579}