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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//! Defines the [`Visitor`] and [`Walk`] traits for traversing a WGSL syntax tree.

use gramatika::Token as _;

use crate::{
	common::{AttributeList, TypeDecl},
	decl::{
		legacy::{ExtensionDecl, ModuleDecl},
		Decl, FieldDecl, FunctionDecl, ImportDecl, ImportPath, ImportPathBlock, ImportPathDecl,
		NamespacedImportPath, ParamDecl, StructDecl, TypeAliasDecl, VarDecl,
	},
	expr::{
		BinaryExpr, BitcastExpr, Expr, FnCallExpr, GroupExpr, IdentExpr, PostfixExpr, PrimaryExpr,
		TypeCtorExpr, UnaryPostExpr, UnaryPreExpr,
	},
	stmt::{
		BlockStmt, CaseStmt, ContinuingStmt, ElseStmt, ExprStmt, ForStmt, IfStmt, LoopStmt,
		ReturnStmt, Stmt, SwitchStmt,
	},
	SyntaxTree, Token,
};

#[derive(Clone, Copy, Default, PartialEq, PartialOrd, Eq, Ord)]
pub enum FlowControl {
	#[default]
	Continue,
	Break,
}

#[allow(unused_variables)]
#[rustfmt::skip]
pub trait Visitor {
	#[must_use] fn visit_decl(&mut self, decl: &Decl) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_stmt(&mut self, stmt: &Stmt) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_expr(&mut self, expr: &Expr) -> FlowControl { FlowControl::Continue }

	#[must_use] fn visit_type(&mut self, decl: &TypeDecl) -> FlowControl { FlowControl::Continue }
	fn visit_attributes(&mut self, attr: &AttributeList) {}

	#[must_use] fn visit_var_decl(&mut self, decl: &VarDecl) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_type_alias_decl(&mut self, decl: &TypeAliasDecl) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_struct_decl(&mut self, decl: &StructDecl) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_field_decl(&mut self, decl: &FieldDecl) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_func_decl(&mut self, decl: &FunctionDecl) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_param_decl(&mut self, decl: &ParamDecl) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_import_path_decl(&mut self, decl: &ImportPathDecl) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_import_decl(&mut self, decl: &ImportDecl) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_import_path(&mut self, path: &ImportPath) -> FlowControl { FlowControl::Continue }
	fn visit_extension_decl(&mut self, decl: &ExtensionDecl) {}
	fn visit_module_decl(&mut self, decl: &ModuleDecl) {}

	#[must_use] fn visit_return_stmt(&mut self, stmt: &ReturnStmt) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_if_stmt(&mut self, stmt: &IfStmt) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_else_stmt(&mut self, stmt: &ElseStmt) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_switch_stmt(&mut self, stmt: &SwitchStmt) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_case_stmt(&mut self, stmt: &CaseStmt) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_loop_stmt(&mut self, stmt: &LoopStmt) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_continuing_stmt(&mut self, stmt: &ContinuingStmt) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_for_stmt(&mut self, stmt: &ForStmt) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_expr_stmt(&mut self, stmt: &ExprStmt) -> FlowControl { FlowControl::Continue }

	#[must_use] fn visit_unary_pre_expr(&mut self, expr: &UnaryPreExpr) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_unary_post_expr(&mut self, expr: &UnaryPostExpr) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_binary_expr(&mut self, expr: &BinaryExpr) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_assignment_expr(&mut self, expr: &BinaryExpr) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_fn_call_expr(&mut self, expr: &FnCallExpr) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_type_ctor_expr(&mut self, expr: &TypeCtorExpr) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_group_expr(&mut self, expr: &GroupExpr) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_bitcast_expr(&mut self, expr: &BitcastExpr) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_primary_expr(&mut self, expr: &PrimaryExpr) -> FlowControl { FlowControl::Continue }
	#[must_use] fn visit_postfix_expr(&mut self, expr: &PostfixExpr) -> FlowControl { FlowControl::Continue }
	fn visit_literal_expr(&mut self, expr: &Token) {}
	fn visit_ident_expr(&mut self, expr: &IdentExpr) {}
}

pub trait Walk {
	fn walk(&self, visitor: &mut dyn Visitor);
}

impl Walk for SyntaxTree {
	fn walk(&self, visitor: &mut dyn Visitor) {
		for decl in self.inner.iter() {
			decl.walk(visitor);
		}
	}
}

// --- Declarations ----------------------------------------------------------------------

impl Walk for Decl {
	fn walk(&self, visitor: &mut dyn Visitor) {
		use Decl::*;

		if visitor.visit_decl(self) == FlowControl::Continue {
			match self {
				Var(decl) | Const(decl) => decl.walk(visitor),
				TypeAlias(decl) => decl.walk(visitor),
				Struct(decl) => decl.walk(visitor),
				Field(decl) => decl.walk(visitor),
				Function(decl) => decl.walk(visitor),
				Param(decl) => decl.walk(visitor),
				Extension(decl) => decl.walk(visitor),
				ImportPath(decl) => decl.walk(visitor),
				Import(decl) => decl.walk(visitor),
				Module(decl) => decl.walk(visitor),
			}
		}
	}
}

impl Walk for VarDecl {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_var_decl(self) == FlowControl::Continue {
			if let Some(ref attributes) = self.attributes {
				visitor.visit_attributes(attributes);
			}

			if let Some(ref ty) = self.ty {
				ty.walk(visitor);
			}

			if let Some(ref expr) = self.assignment {
				expr.walk(visitor);
			}
		}
	}
}

impl Walk for TypeAliasDecl {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_type_alias_decl(self) == FlowControl::Continue {
			self.value.walk(visitor);
		}
	}
}

impl Walk for StructDecl {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_struct_decl(self) == FlowControl::Continue {
			for field in self.body.fields.iter() {
				field.walk(visitor);
			}
		}
	}
}

impl Walk for FieldDecl {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_field_decl(self) == FlowControl::Continue {
			self.ty.walk(visitor);
		}
	}
}

impl Walk for FunctionDecl {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_func_decl(self) == FlowControl::Continue {
			for param in self.params.iter() {
				param.walk(visitor);
			}

			if let Some(ref ty) = self.return_ty {
				ty.walk(visitor);
			}

			self.body.walk(visitor);
		}
	}
}

impl Walk for ParamDecl {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_param_decl(self) == FlowControl::Continue {
			self.ty.walk(visitor);
		}
	}
}

impl Walk for ExtensionDecl {
	fn walk(&self, visitor: &mut dyn Visitor) {
		visitor.visit_extension_decl(self);
	}
}

impl Walk for ImportPathDecl {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_import_path_decl(self) == FlowControl::Continue {
			self.path.walk(visitor);
		}
	}
}

impl Walk for ImportDecl {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_import_decl(self) == FlowControl::Continue {
			self.path.walk(visitor);
		}
	}
}

impl Walk for ImportPath {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_import_path(self) == FlowControl::Continue {
			match self {
				ImportPath::Namespaced(NamespacedImportPath { path, .. }) => {
					path.walk(visitor);
				}
				ImportPath::Block(ImportPathBlock { paths, .. }) => {
					for path in paths.iter() {
						path.walk(visitor);
					}
				}
				ImportPath::Leaf(_) => {}
			}
		}
	}
}

impl Walk for ModuleDecl {
	fn walk(&self, visitor: &mut dyn Visitor) {
		visitor.visit_module_decl(self);
	}
}

// --- Statements ------------------------------------------------------------------------

impl Walk for Stmt {
	fn walk(&self, visitor: &mut dyn Visitor) {
		use Stmt::*;

		if visitor.visit_stmt(self) == FlowControl::Continue {
			match self {
				Block(stmt) => stmt.walk(visitor),
				Return(stmt) => stmt.walk(visitor),
				If(stmt) => stmt.walk(visitor),
				Switch(stmt) => stmt.walk(visitor),
				Loop(stmt) => stmt.walk(visitor),
				Continuing(stmt) => stmt.walk(visitor),
				For(stmt) => stmt.walk(visitor),
				Var(decl) => decl.walk(visitor),
				Expr(stmt) => stmt.walk(visitor),
				_ => {}
			}
		}
	}
}

impl Walk for BlockStmt {
	fn walk(&self, visitor: &mut dyn Visitor) {
		for stmt in self.stmts.iter() {
			stmt.walk(visitor);
		}
	}
}

impl Walk for ReturnStmt {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_return_stmt(self) == FlowControl::Continue {
			if let Some(ref expr) = self.value {
				expr.walk(visitor);
			}
		}
	}
}

impl Walk for IfStmt {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_if_stmt(self) == FlowControl::Continue {
			self.condition.walk(visitor);
			self.then_branch.walk(visitor);

			if let Some(ref stmt) = self.else_branch {
				stmt.walk(visitor);
			}
		}
	}
}

impl Walk for ElseStmt {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_else_stmt(self) == FlowControl::Continue {
			self.body.walk(visitor);
		}
	}
}

impl Walk for SwitchStmt {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_switch_stmt(self) == FlowControl::Continue {
			self.subject.walk(visitor);

			for stmt in self.body.cases.iter() {
				stmt.walk(visitor);
			}
		}
	}
}

impl Walk for CaseStmt {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_case_stmt(self) == FlowControl::Continue {
			for expr in self.selectors.iter() {
				expr.walk(visitor);
			}
			self.body.walk(visitor);
		}
	}
}

impl Walk for LoopStmt {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_loop_stmt(self) == FlowControl::Continue {
			self.body.walk(visitor);
		}
	}
}

impl Walk for ContinuingStmt {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_continuing_stmt(self) == FlowControl::Continue {
			self.body.walk(visitor);
		}
	}
}

impl Walk for ForStmt {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_for_stmt(self) == FlowControl::Continue {
			if let Some(ref stmt) = self.initializer {
				stmt.walk(visitor);
			}

			if let Some(ref expr) = self.condition {
				expr.walk(visitor);
			}

			if let Some(ref expr) = self.increment {
				expr.walk(visitor);
			}

			self.body.walk(visitor);
		}
	}
}

impl Walk for ExprStmt {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_expr_stmt(self) == FlowControl::Continue {
			self.expr.walk(visitor);
		}
	}
}

// --- Expressions -----------------------------------------------------------------------

impl Walk for Expr {
	fn walk(&self, visitor: &mut dyn Visitor) {
		use Expr::*;

		if visitor.visit_expr(self) == FlowControl::Continue {
			match self {
				UnaryPre(expr) => expr.walk(visitor),
				UnaryPost(expr) => expr.walk(visitor),
				Binary(expr) => expr.walk(visitor),
				Assignment(expr) => expr.walk(visitor),
				FnCall(expr) => expr.walk(visitor),
				TypeCtor(expr) => expr.walk(visitor),
				Group(expr) => expr.walk(visitor),
				Bitcast(expr) => expr.walk(visitor),
				Literal(expr) => visitor.visit_literal_expr(expr),
				Ident(expr) => visitor.visit_ident_expr(expr),
				Primary(expr) => expr.walk(visitor),
			};
		}
	}
}

impl Walk for UnaryPreExpr {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_unary_pre_expr(self) == FlowControl::Continue {
			self.expr.walk(visitor);
		}
	}
}

impl Walk for UnaryPostExpr {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_unary_post_expr(self) == FlowControl::Continue {
			self.expr.walk(visitor);
		}
	}
}

impl Walk for BinaryExpr {
	fn walk(&self, visitor: &mut dyn Visitor) {
		let visit_method = match self.op.lexeme().as_str() {
			"=" => Visitor::visit_assignment_expr,
			_ => Visitor::visit_binary_expr,
		};

		if visit_method(visitor, self) == FlowControl::Continue {
			self.lhs.walk(visitor);
			self.rhs.walk(visitor);
		}
	}
}

impl Walk for FnCallExpr {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_fn_call_expr(self) == FlowControl::Continue {
			visitor.visit_ident_expr(&self.ident);

			for arg in self.arguments.arguments.iter() {
				arg.walk(visitor);
			}
		}
	}
}

impl Walk for TypeCtorExpr {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_type_ctor_expr(self) == FlowControl::Continue {
			self.ty.walk(visitor);

			for arg in self.arguments.arguments.iter() {
				arg.walk(visitor);
			}
		}
	}
}

impl Walk for GroupExpr {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_group_expr(self) == FlowControl::Continue {
			self.expr.walk(visitor);
		}
	}
}

impl Walk for BitcastExpr {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_bitcast_expr(self) == FlowControl::Continue {
			self.ty.walk(visitor);
			self.expr.walk(visitor);
		}
	}
}

impl Walk for PrimaryExpr {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_primary_expr(self) == FlowControl::Continue {
			self.expr.walk(visitor);

			if let Some(ref expr) = self.postfix {
				expr.walk(visitor);
			}
		}
	}
}

impl Walk for PostfixExpr {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_postfix_expr(self) == FlowControl::Continue {
			self.expr.walk(visitor);

			if let Some(ref expr) = self.postfix {
				expr.walk(visitor);
			}
		}
	}
}

// --- Common ----------------------------------------------------------------------------

impl Walk for TypeDecl {
	fn walk(&self, visitor: &mut dyn Visitor) {
		if visitor.visit_type(self) == FlowControl::Continue {
			if let Some(ref attributes) = self.attributes {
				visitor.visit_attributes(attributes);
			}

			if let Some(ref ty) = self.child_ty {
				ty.walk(visitor);
			}

			visitor.visit_ident_expr(&self.name);
		}
	}
}