dive_model/intern.rs
1//! The run's name table, fed in TLC's encounter order.
2//!
3//! TLC orders strings, model values and record fields by their token in
4//! `util.UniqueString`'s table, and fingerprints a model value by its token.
5//! Measured on the pinned TLC (`acceptance/ModelDump.java` reads the table
6//! after TLC has processed the model), the table is filled in this order:
7//!
8//! 1. the configuration's values, strings and model values alike, in the order
9//! TLC's `ModelConfig.parse` reads them (it runs before SANY). Token 0 is
10//! never used, so the first value has token 1;
11//! 2. a fixed set of names SANY creates before it parses (`PREFIX` in `tlc_names.rs`);
12//! 3. the image of every token SANY's lexer reads, file by file in parse
13//! order, with SANY's syntax-node kind names (`NODE_KINDS`) after the
14//! root module's header line ([`dive_ir::Ir::lexical_names`]);
15//! 4. names SANY's semantic analysis creates: a few internal names, then the
16//! contents of string literals as it builds their `StringNode`s, module by
17//! module in semantic-analysis order and in source order within a module
18//! ([`semantic_strings`]);
19//! 5. strings and names created while TLC runs.
20//!
21//! Model values come first, so their tokens are TLC's exactly. The relative
22//! order of every other name is TLC's as long as SANY does not intern it
23//! under a different image (some operator forms) or in a step not listed
24//! here; `acceptance/README.md` records how close the feed is on the corpus.
25
26use dive_config::{ConfigValue, ConstantEntry, ModelConfig};
27use dive_ir::Ir;
28use dive_value::Interner;
29
30use crate::tlc_names::{NODE_KINDS, PREFIX};
31
32/// A name TLC never interns, standing for its unused token 0.
33const TOKEN_ZERO: &str = "\u{0}dive: token 0\u{0}";
34
35/// Names SANY's semantic analysis interns after parsing, before TLC
36/// processes the constants.
37const SEMANTIC: &[&str] = &["$$InAssume", "nullODN", "nullOpArg", "nullLabelNode"];
38
39/// The name table for a run: everything TLC interns before it evaluates
40/// anything.
41pub(crate) fn feed(ir: &Ir, cfg: &ModelConfig) -> Interner {
42 let names = Interner::new();
43 names.intern(TOKEN_ZERO);
44 for e in &cfg.constants {
45 if let ConstantEntry::Assignment { args, value, .. } = e {
46 for a in args {
47 cfg_value(&names, &a.value);
48 }
49 cfg_value(&names, &value.value);
50 }
51 }
52 for p in PREFIX {
53 names.intern(p);
54 }
55 let lexical = ir.lexical_names();
56 // The root module's header line is its first three tokens (`---- MODULE`,
57 // the name, `----`) in TLC's lexer; dive's lexer may split the first.
58 let header = header_len(lexical);
59 for n in &lexical[..header] {
60 names.intern(n);
61 }
62 for k in NODE_KINDS {
63 names.intern(k);
64 }
65 for n in &lexical[header..] {
66 names.intern(n);
67 }
68 for s in SEMANTIC {
69 names.intern(s);
70 }
71 for s in semantic_strings(ir) {
72 names.intern(&s);
73 }
74 names
75}
76
77/// The contents of every string literal, in the order SANY's semantic
78/// analysis creates their `StringNode`s (each interns its contents): the
79/// file-level modules in semantic-analysis order (the IR's module order),
80/// and within each file in source order (inner modules are analysed where
81/// they appear).
82#[must_use]
83pub fn semantic_strings(ir: &Ir) -> Vec<Box<str>> {
84 let rank: std::collections::HashMap<dive_ir::FileId, usize> = ir
85 .modules()
86 .iter()
87 .filter(|m| m.nesting == 0)
88 .filter_map(|m| m.file)
89 .enumerate()
90 .map(|(i, f)| (f, i))
91 .collect();
92 let mut found: Vec<(usize, u32, &str)> = ir
93 .nodes()
94 .iter()
95 .filter_map(|n| match &n.expr {
96 dive_ir::Expr::Str(s) => {
97 let f = n.origin.file?;
98 Some((*rank.get(&f)?, n.origin.range.0, &**s))
99 }
100 _ => None,
101 })
102 .collect();
103 found.sort_by_key(|&(r, at, _)| (r, at));
104 let mut seen = std::collections::HashSet::new();
105 found
106 .into_iter()
107 .filter(|(_, _, s)| seen.insert(*s))
108 .map(|(_, _, s)| s.into())
109 .collect()
110}
111
112/// How many of the first lexical names make up the root module's header.
113fn header_len(lexical: &[Box<str>]) -> usize {
114 // TLC's lexer reads `---- MODULE` as one token; dive's may read the dashes
115 // and `MODULE` apart. The header ends at the second run of dashes.
116 let mut dashes = 0;
117 for (i, n) in lexical.iter().enumerate() {
118 if n.starts_with("----") {
119 dashes += 1;
120 if dashes == 2 {
121 return i + 1;
122 }
123 }
124 }
125 0
126}
127
128fn cfg_value(names: &Interner, v: &ConfigValue) {
129 match v {
130 ConfigValue::String(s) | ConfigValue::ModelValue(s) => {
131 names.intern(s);
132 }
133 ConfigValue::Set(items) => {
134 for i in items {
135 cfg_value(names, &i.value);
136 }
137 }
138 ConfigValue::Int(_) | ConfigValue::Bool(_) => {}
139 }
140}