1use crate::catalog::{Catalog, Kind};
8use crate::settings::SettingsNode;
9use crate::settings::table;
10use crate::source::Span;
11use crate::wir::{Action, Program, Value};
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum IncompletenessKind {
15 RawSetting,
16 UnknownAction,
17 UnknownValue,
18 OpaqueAction,
19}
20
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub enum ResidualClassification {
23 ProjectDefinedConstruct,
24 SourceDeclaredVariable,
25 ProducerExtension,
26 LegacyOpaque,
27 UnresolvedIdentifier,
28}
29
30impl ResidualClassification {
31 pub fn as_str(self) -> &'static str {
32 match self {
33 Self::ProjectDefinedConstruct => "project-defined-construct",
34 Self::SourceDeclaredVariable => "source-declared-variable",
35 Self::ProducerExtension => "producer-extension",
36 Self::LegacyOpaque => "legacy-opaque-construct",
37 Self::UnresolvedIdentifier => "truly-unresolved-identifier",
38 }
39 }
40
41 pub fn evidence(self) -> &'static str {
42 match self {
43 Self::ProjectDefinedConstruct => {
44 "source settings or construct was preserved without a canonical catalog identity"
45 }
46 Self::SourceDeclaredVariable => {
47 "the identifier matches a variable declaration in the parsed source program"
48 }
49 Self::ProducerExtension => {
50 "the source uses an action-shaped identity outside the canonical catalog and no declaration resolves it"
51 }
52 Self::LegacyOpaque => {
53 "the parser preserved a legacy raw construct without a canonical contract"
54 }
55 Self::UnresolvedIdentifier => {
56 "the identifier matches neither a source declaration nor a canonical catalog identity"
57 }
58 }
59 }
60}
61
62#[derive(Debug, Clone, PartialEq, Eq)]
63pub struct SemanticIssue {
64 pub kind: IncompletenessKind,
65 pub name: String,
66 pub span: Option<Span>,
67 pub classification: ResidualClassification,
68}
69
70pub fn inspect(program: &Program, catalog: &Catalog) -> Vec<SemanticIssue> {
73 let mut issues = Vec::new();
74 if let Some(settings) = &program.settings {
75 for node in &settings.children {
76 inspect_setting(node, &mut issues);
77 }
78 }
79 for action in program.actions.iter() {
80 inspect_action(action, program, catalog, &mut issues);
81 }
82 for value in program.values.iter() {
83 inspect_value(value, program, catalog, &mut issues);
84 }
85 issues
86}
87
88fn inspect_setting(node: &SettingsNode, issues: &mut Vec<SemanticIssue>) {
89 match node {
90 SettingsNode::Workshop { .. } => {}
91 SettingsNode::Group { children, .. } => {
92 for child in children {
93 inspect_setting(child, issues);
94 }
95 }
96 SettingsNode::Raw { name, span, .. } => issues.push(SemanticIssue {
97 kind: IncompletenessKind::RawSetting,
98 name: name.clone(),
99 span: *span,
100 classification: ResidualClassification::ProjectDefinedConstruct,
101 }),
102 SettingsNode::List {
103 name,
104 elements,
105 span,
106 } => {
107 let known = match name.as_str() {
108 "enabledMaps" | "disabledMaps" => elements
109 .iter()
110 .all(|element| table::map_name(&element.value).is_some()),
111 "enabledHeroes" | "disabledHeroes" => elements
112 .iter()
113 .all(|element| table::hero_name(&element.value).is_some()),
114 _ => true,
115 };
116 if !known {
117 issues.push(SemanticIssue {
118 kind: IncompletenessKind::RawSetting,
119 name: name.clone(),
120 span: *span,
121 classification: ResidualClassification::ProjectDefinedConstruct,
122 });
123 }
124 }
125 SettingsNode::Number { .. }
126 | SettingsNode::Bool { .. }
127 | SettingsNode::Flag { .. }
128 | SettingsNode::String { .. } => {}
129 }
130}
131
132fn inspect_action(
133 action: &Action,
134 program: &Program,
135 catalog: &Catalog,
136 issues: &mut Vec<SemanticIssue>,
137) {
138 match action {
139 Action::Call { name, span, .. } => {
140 let kind = if name == "rawWorkshopAction" {
141 Some(IncompletenessKind::OpaqueAction)
142 } else if catalog.entry(Kind::Action, name).is_none() {
143 Some(IncompletenessKind::UnknownAction)
144 } else {
145 None
146 };
147 if let Some(kind) = kind {
148 let classification = if kind == IncompletenessKind::OpaqueAction {
149 ResidualClassification::LegacyOpaque
150 } else {
151 ResidualClassification::ProducerExtension
152 };
153 issues.push(SemanticIssue {
154 kind,
155 name: name.clone(),
156 span: *span,
157 classification,
158 });
159 }
160 }
161 Action::If {
162 branches,
163 else_body,
164 ..
165 } => {
166 for branch in branches {
167 inspect_action_id(branch.body.as_slice(), program, catalog, issues);
168 }
169 if let Some(body) = else_body {
170 inspect_action_id(body.as_slice(), program, catalog, issues);
171 }
172 }
173 Action::While { body, .. }
174 | Action::ForGlobalVariable { body, .. }
175 | Action::ForPlayerVariable { body, .. } => {
176 inspect_action_id(body.as_slice(), program, catalog, issues);
177 }
178 Action::SetGlobalVariable { .. }
179 | Action::ModifyGlobalVariable { .. }
180 | Action::SetPlayerVariable { .. }
181 | Action::ModifyPlayerVariable { .. }
182 | Action::AssignMember { .. }
183 | Action::CallSubroutine { .. } => {}
184 }
185}
186
187fn inspect_action_id(
188 ids: &[crate::wir::ActionId],
189 program: &Program,
190 catalog: &Catalog,
191 issues: &mut Vec<SemanticIssue>,
192) {
193 for id in ids {
194 if let Some(action) = program.actions.get(*id) {
195 inspect_action(action, program, catalog, issues);
196 }
197 }
198}
199
200fn inspect_value(
201 node: &crate::wir::ValueNode,
202 program: &Program,
203 catalog: &Catalog,
204 issues: &mut Vec<SemanticIssue>,
205) {
206 if let Value::Call { name, args } = &node.value {
207 let canonical_helper = matches!(
211 name.as_str(),
212 "memberAccess"
213 | "+"
214 | "-"
215 | "*"
216 | "/"
217 | "%"
218 | "add"
219 | "subtract"
220 | "multiply"
221 | "divide"
222 | "modulo"
223 | "min"
224 | "max"
225 | "raiseToPower"
226 | "appendToArray"
227 | "removeFromArray"
228 | "removeFromArrayByIndex"
229 ) && (args.is_empty()
230 || matches!(name.as_str(), "memberAccess" | "+" | "-" | "*" | "/" | "%"));
231 if canonical_helper {
232 return;
233 }
234 if catalog.entry(Kind::Value, name).is_none()
235 && catalog.entry(Kind::Operator, name).is_none()
236 {
237 issues.push(SemanticIssue {
238 kind: IncompletenessKind::UnknownValue,
239 name: name.clone(),
240 span: node.span,
241 classification: if program
242 .global_variables
243 .iter()
244 .any(|variable| variable.name == *name)
245 || program
246 .player_variables
247 .iter()
248 .any(|variable| variable.name == *name)
249 {
250 ResidualClassification::SourceDeclaredVariable
251 } else {
252 ResidualClassification::UnresolvedIdentifier
253 },
254 });
255 }
256 }
257}
258
259#[cfg(test)]
260mod tests {
261 use super::*;
262 use crate::settings::{Settings, SettingsNode};
263
264 #[test]
265 fn reports_preserved_and_unknown_nodes() {
266 let catalog = Catalog::builtin().expect("builtin catalog");
267 let mut program = Program {
268 settings: Some(Settings {
269 span: None,
270 children: vec![SettingsNode::Raw {
271 name: "Future Setting".to_string(),
272 value: "opaque".to_string(),
273 span: None,
274 }],
275 }),
276 ..Program::default()
277 };
278 program.actions.push(Action::Call {
279 name: "rawWorkshopAction".to_string(),
280 args: Vec::new(),
281 span: None,
282 });
283 program.actions.push(Action::Call {
284 name: "futureAction".to_string(),
285 args: Vec::new(),
286 span: None,
287 });
288 program.values.push(crate::wir::ValueNode::new(
289 Value::Call {
290 name: "futureValue".to_string(),
291 args: Vec::new(),
292 },
293 None,
294 ));
295
296 let issues = inspect(&program, &catalog);
297 assert!(
298 issues
299 .iter()
300 .any(|issue| issue.kind == IncompletenessKind::RawSetting)
301 );
302 assert!(
303 issues
304 .iter()
305 .any(|issue| issue.kind == IncompletenessKind::OpaqueAction)
306 );
307 assert!(
308 issues
309 .iter()
310 .any(|issue| issue.kind == IncompletenessKind::UnknownAction)
311 );
312 assert!(
313 issues
314 .iter()
315 .any(|issue| issue.kind == IncompletenessKind::UnknownValue)
316 );
317 assert!(issues.iter().any(|issue| {
318 issue.kind == IncompletenessKind::RawSetting
319 && issue.classification == ResidualClassification::ProjectDefinedConstruct
320 }));
321 assert!(issues.iter().any(|issue| {
322 issue.kind == IncompletenessKind::OpaqueAction
323 && issue.classification == ResidualClassification::LegacyOpaque
324 }));
325 assert!(issues.iter().any(|issue| {
326 issue.kind == IncompletenessKind::UnknownValue
327 && issue.classification == ResidualClassification::UnresolvedIdentifier
328 }));
329 }
330}