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
//! A [visitor](Visit) (implementer of the [Visit] trait) walks the immutable AST, mutating itself.

use crate::ast::*;
use cl_structures::span::Span;

/// Immutably walks the entire AST
///
/// Each method acts as a customization point.
///
/// There are a set of default implementations for enums
/// under the name [`or_visit_`*](or_visit_expr_kind),
/// provided for ease of use.
///
/// For all other nodes, traversal is *explicit*.
pub trait Visit<'a>: Sized {
    fn visit_span(&mut self, _extents: &'a Span) {}
    fn visit_mutability(&mut self, _mutable: &'a Mutability) {}
    fn visit_visibility(&mut self, _vis: &'a Visibility) {}
    fn visit_sym(&mut self, _name: &'a Sym) {}
    fn visit_literal(&mut self, l: &'a Literal) {
        or_visit_literal(self, l)
    }
    fn visit_bool(&mut self, _b: &'a bool) {}
    fn visit_char(&mut self, _c: &'a char) {}
    fn visit_int(&mut self, _i: &'a u128) {}
    fn visit_string(&mut self, _s: &'a str) {}
    fn visit_file(&mut self, f: &'a File) {
        let File { items } = f;
        items.iter().for_each(|i| self.visit_item(i));
    }
    fn visit_attrs(&mut self, a: &'a Attrs) {
        let Attrs { meta } = a;
        meta.iter().for_each(|m| self.visit_meta(m));
    }
    fn visit_meta(&mut self, m: &'a Meta) {
        let Meta { name, kind } = m;
        self.visit_sym(name);
        self.visit_meta_kind(kind);
    }
    fn visit_meta_kind(&mut self, kind: &'a MetaKind) {
        or_visit_meta_kind(self, kind)
    }
    fn visit_item(&mut self, i: &'a Item) {
        let Item { extents, attrs, vis, kind } = i;
        self.visit_span(extents);
        self.visit_attrs(attrs);
        self.visit_visibility(vis);
        self.visit_item_kind(kind);
    }
    fn visit_item_kind(&mut self, kind: &'a ItemKind) {
        or_visit_item_kind(self, kind)
    }
    fn visit_alias(&mut self, a: &'a Alias) {
        let Alias { to, from } = a;
        self.visit_sym(to);
        if let Some(t) = from {
            self.visit_ty(t)
        }
    }
    fn visit_const(&mut self, c: &'a Const) {
        let Const { name, ty, init } = c;
        self.visit_sym(name);
        self.visit_ty(ty);
        self.visit_expr(init);
    }
    fn visit_static(&mut self, s: &'a Static) {
        let Static { mutable, name, ty, init } = s;
        self.visit_mutability(mutable);
        self.visit_sym(name);
        self.visit_ty(ty);
        self.visit_expr(init);
    }
    fn visit_module(&mut self, m: &'a Module) {
        let Module { name, kind } = m;
        self.visit_sym(name);
        self.visit_module_kind(kind);
    }
    fn visit_module_kind(&mut self, kind: &'a ModuleKind) {
        or_visit_module_kind(self, kind)
    }
    fn visit_function(&mut self, f: &'a Function) {
        let Function { name, sign, bind, body } = f;
        self.visit_sym(name);
        self.visit_ty_fn(sign);
        bind.iter().for_each(|p| self.visit_param(p));
        if let Some(b) = body {
            self.visit_block(b)
        }
    }
    fn visit_param(&mut self, p: &'a Param) {
        let Param { mutability, name } = p;
        self.visit_mutability(mutability);
        self.visit_sym(name);
    }
    fn visit_struct(&mut self, s: &'a Struct) {
        let Struct { name, kind } = s;
        self.visit_sym(name);
        self.visit_struct_kind(kind);
    }
    fn visit_struct_kind(&mut self, kind: &'a StructKind) {
        or_visit_struct_kind(self, kind)
    }
    fn visit_struct_member(&mut self, m: &'a StructMember) {
        let StructMember { vis, name, ty } = m;
        self.visit_visibility(vis);
        self.visit_sym(name);
        self.visit_ty(ty);
    }
    fn visit_enum(&mut self, e: &'a Enum) {
        let Enum { name, kind } = e;
        self.visit_sym(name);
        self.visit_enum_kind(kind);
    }
    fn visit_enum_kind(&mut self, kind: &'a EnumKind) {
        or_visit_enum_kind(self, kind)
    }
    fn visit_variant(&mut self, v: &'a Variant) {
        let Variant { name, kind } = v;
        self.visit_sym(name);
        self.visit_variant_kind(kind);
    }
    fn visit_variant_kind(&mut self, kind: &'a VariantKind) {
        or_visit_variant_kind(self, kind)
    }
    fn visit_impl(&mut self, i: &'a Impl) {
        let Impl { target, body } = i;
        self.visit_impl_kind(target);
        self.visit_file(body);
    }
    fn visit_impl_kind(&mut self, target: &'a ImplKind) {
        or_visit_impl_kind(self, target)
    }
    fn visit_use(&mut self, u: &'a Use) {
        let Use { absolute: _, tree } = u;
        self.visit_use_tree(tree);
    }
    fn visit_use_tree(&mut self, tree: &'a UseTree) {
        or_visit_use_tree(self, tree)
    }
    fn visit_ty(&mut self, t: &'a Ty) {
        let Ty { extents, kind } = t;
        self.visit_span(extents);
        self.visit_ty_kind(kind);
    }
    fn visit_ty_kind(&mut self, kind: &'a TyKind) {
        or_visit_ty_kind(self, kind)
    }
    fn visit_ty_tuple(&mut self, t: &'a TyTuple) {
        let TyTuple { types } = t;
        types.iter().for_each(|kind| self.visit_ty_kind(kind))
    }
    fn visit_ty_ref(&mut self, t: &'a TyRef) {
        let TyRef { mutable, count: _, to } = t;
        self.visit_mutability(mutable);
        self.visit_path(to);
    }
    fn visit_ty_fn(&mut self, t: &'a TyFn) {
        let TyFn { args, rety } = t;
        self.visit_ty_kind(args);
        if let Some(rety) = rety {
            self.visit_ty(rety);
        }
    }
    fn visit_path(&mut self, p: &'a Path) {
        let Path { absolute: _, parts } = p;
        parts.iter().for_each(|p| self.visit_path_part(p))
    }
    fn visit_path_part(&mut self, p: &'a PathPart) {
        match p {
            PathPart::SuperKw => {}
            PathPart::SelfKw => {}
            PathPart::Ident(i) => self.visit_sym(i),
        }
    }
    fn visit_stmt(&mut self, s: &'a Stmt) {
        let Stmt { extents, kind, semi } = s;
        self.visit_span(extents);
        self.visit_stmt_kind(kind);
        self.visit_semi(semi);
    }
    fn visit_stmt_kind(&mut self, kind: &'a StmtKind) {
        or_visit_stmt_kind(self, kind)
    }
    fn visit_semi(&mut self, _s: &'a Semi) {}
    fn visit_let(&mut self, l: &'a Let) {
        let Let { mutable, name, ty, init } = l;
        self.visit_mutability(mutable);
        self.visit_sym(name);
        if let Some(ty) = ty {
            self.visit_ty(ty);
        }
        if let Some(init) = init {
            self.visit_expr(init)
        }
    }
    fn visit_expr(&mut self, e: &'a Expr) {
        let Expr { extents, kind } = e;
        self.visit_span(extents);
        self.visit_expr_kind(kind)
    }
    fn visit_expr_kind(&mut self, e: &'a ExprKind) {
        or_visit_expr_kind(self, e)
    }
    fn visit_assign(&mut self, a: &'a Assign) {
        let Assign { parts } = a;
        let (head, tail) = parts.as_ref();
        self.visit_expr_kind(head);
        self.visit_expr_kind(tail);
    }
    fn visit_modify(&mut self, m: &'a Modify) {
        let Modify { kind, parts } = m;
        let (head, tail) = parts.as_ref();
        self.visit_modify_kind(kind);
        self.visit_expr_kind(head);
        self.visit_expr_kind(tail);
    }
    fn visit_modify_kind(&mut self, _kind: &'a ModifyKind) {}
    fn visit_binary(&mut self, b: &'a Binary) {
        let Binary { kind, parts } = b;
        let (head, tail) = parts.as_ref();
        self.visit_binary_kind(kind);
        self.visit_expr_kind(head);
        self.visit_expr_kind(tail);
    }
    fn visit_binary_kind(&mut self, _kind: &'a BinaryKind) {}
    fn visit_unary(&mut self, u: &'a Unary) {
        let Unary { kind, tail } = u;
        self.visit_unary_kind(kind);
        self.visit_expr_kind(tail);
    }
    fn visit_unary_kind(&mut self, _kind: &'a UnaryKind) {}
    fn visit_member(&mut self, m: &'a Member) {
        let Member { head, kind } = m;
        self.visit_expr_kind(head);
        self.visit_member_kind(kind);
    }
    fn visit_member_kind(&mut self, kind: &'a MemberKind) {
        or_visit_member_kind(self, kind)
    }
    fn visit_index(&mut self, i: &'a Index) {
        let Index { head, indices } = i;
        self.visit_expr_kind(head);
        indices.iter().for_each(|e| self.visit_expr(e));
    }
    fn visit_structor(&mut self, s: &'a Structor) {
        let Structor { to, init } = s;
        self.visit_path(to);
        init.iter().for_each(|e| self.visit_fielder(e))
    }
    fn visit_fielder(&mut self, f: &'a Fielder) {
        let Fielder { name, init } = f;
        self.visit_sym(name);
        if let Some(init) = init {
            self.visit_expr(init);
        }
    }
    fn visit_array(&mut self, a: &'a Array) {
        let Array { values } = a;
        values.iter().for_each(|e| self.visit_expr(e))
    }
    fn visit_array_rep(&mut self, a: &'a ArrayRep) {
        let ArrayRep { value, repeat } = a;
        self.visit_expr_kind(value);
        self.visit_expr_kind(repeat);
    }
    fn visit_addrof(&mut self, a: &'a AddrOf) {
        let AddrOf { count: _, mutable, expr } = a;
        self.visit_mutability(mutable);
        self.visit_expr_kind(expr);
    }
    fn visit_block(&mut self, b: &'a Block) {
        let Block { stmts } = b;
        stmts.iter().for_each(|s| self.visit_stmt(s));
    }
    fn visit_group(&mut self, g: &'a Group) {
        let Group { expr } = g;
        self.visit_expr_kind(expr)
    }
    fn visit_tuple(&mut self, t: &'a Tuple) {
        let Tuple { exprs } = t;
        exprs.iter().for_each(|e| self.visit_expr(e))
    }
    fn visit_loop(&mut self, l: &'a Loop) {
        let Loop { body } = l;
        self.visit_expr(body)
    }
    fn visit_while(&mut self, w: &'a While) {
        let While { cond, pass, fail } = w;
        self.visit_expr(cond);
        self.visit_block(pass);
        self.visit_else(fail);
    }
    fn visit_if(&mut self, i: &'a If) {
        let If { cond, pass, fail } = i;
        self.visit_expr(cond);
        self.visit_block(pass);
        self.visit_else(fail);
    }
    fn visit_for(&mut self, f: &'a For) {
        let For { bind, cond, pass, fail } = f;
        self.visit_sym(bind);
        self.visit_expr(cond);
        self.visit_block(pass);
        self.visit_else(fail);
    }
    fn visit_else(&mut self, e: &'a Else) {
        let Else { body } = e;
        if let Some(body) = body {
            self.visit_expr(body)
        }
    }
    fn visit_break(&mut self, b: &'a Break) {
        let Break { body } = b;
        if let Some(body) = body {
            self.visit_expr(body)
        }
    }
    fn visit_return(&mut self, r: &'a Return) {
        let Return { body } = r;
        if let Some(body) = body {
            self.visit_expr(body)
        }
    }
    fn visit_continue(&mut self, c: &'a Continue) {
        let Continue = c;
    }
}

pub fn or_visit_literal<'a, V: Visit<'a>>(visitor: &mut V, l: &'a Literal) {
    match l {
        Literal::Bool(b) => visitor.visit_bool(b),
        Literal::Char(c) => visitor.visit_char(c),
        Literal::Int(i) => visitor.visit_int(i),
        Literal::String(s) => visitor.visit_string(s),
    }
}

pub fn or_visit_meta_kind<'a, V: Visit<'a>>(visitor: &mut V, kind: &'a MetaKind) {
    match kind {
        MetaKind::Plain => {}
        MetaKind::Equals(l) => visitor.visit_literal(l),
        MetaKind::Func(lits) => lits.iter().for_each(|l| visitor.visit_literal(l)),
    }
}

pub fn or_visit_item_kind<'a, V: Visit<'a>>(visitor: &mut V, kind: &'a ItemKind) {
    match kind {
        ItemKind::Module(m) => visitor.visit_module(m),
        ItemKind::Alias(a) => visitor.visit_alias(a),
        ItemKind::Enum(e) => visitor.visit_enum(e),
        ItemKind::Struct(s) => visitor.visit_struct(s),
        ItemKind::Const(c) => visitor.visit_const(c),
        ItemKind::Static(s) => visitor.visit_static(s),
        ItemKind::Function(f) => visitor.visit_function(f),
        ItemKind::Impl(i) => visitor.visit_impl(i),
        ItemKind::Use(u) => visitor.visit_use(u),
    }
}

pub fn or_visit_module_kind<'a, V: Visit<'a>>(visitor: &mut V, kind: &'a ModuleKind) {
    match kind {
        ModuleKind::Inline(f) => visitor.visit_file(f),
        ModuleKind::Outline => {}
    }
}

pub fn or_visit_struct_kind<'a, V: Visit<'a>>(visitor: &mut V, kind: &'a StructKind) {
    match kind {
        StructKind::Empty => {}
        StructKind::Tuple(ty) => ty.iter().for_each(|t| visitor.visit_ty(t)),
        StructKind::Struct(m) => m.iter().for_each(|m| visitor.visit_struct_member(m)),
    }
}

pub fn or_visit_enum_kind<'a, V: Visit<'a>>(visitor: &mut V, kind: &'a EnumKind) {
    match kind {
        EnumKind::NoVariants => {}
        EnumKind::Variants(variants) => variants.iter().for_each(|v| visitor.visit_variant(v)),
    }
}

pub fn or_visit_variant_kind<'a, V: Visit<'a>>(visitor: &mut V, kind: &'a VariantKind) {
    match kind {
        VariantKind::Plain => {}
        VariantKind::CLike(_) => {}
        VariantKind::Tuple(t) => visitor.visit_ty(t),
        VariantKind::Struct(m) => m.iter().for_each(|m| visitor.visit_struct_member(m)),
    }
}

pub fn or_visit_impl_kind<'a, V: Visit<'a>>(visitor: &mut V, target: &'a ImplKind) {
    match target {
        ImplKind::Type(t) => visitor.visit_ty(t),
        ImplKind::Trait { impl_trait, for_type } => {
            visitor.visit_path(impl_trait);
            visitor.visit_ty(for_type)
        }
    }
}

pub fn or_visit_use_tree<'a, V: Visit<'a>>(visitor: &mut V, tree: &'a UseTree) {
    match tree {
        UseTree::Tree(tree) => {
            tree.iter().for_each(|tree| visitor.visit_use_tree(tree));
        }
        UseTree::Path(path, rest) => {
            visitor.visit_path_part(path);
            visitor.visit_use_tree(rest)
        }
        UseTree::Alias(path, name) => {
            visitor.visit_sym(path);
            visitor.visit_sym(name);
        }
        UseTree::Name(name) => {
            visitor.visit_sym(name);
        }
        UseTree::Glob => {}
    }
}

pub fn or_visit_ty_kind<'a, V: Visit<'a>>(visitor: &mut V, kind: &'a TyKind) {
    match kind {
        TyKind::Never => {}
        TyKind::Empty => {}
        TyKind::SelfTy => {}
        TyKind::Path(p) => visitor.visit_path(p),
        TyKind::Tuple(t) => visitor.visit_ty_tuple(t),
        TyKind::Ref(t) => visitor.visit_ty_ref(t),
        TyKind::Fn(t) => visitor.visit_ty_fn(t),
    }
}

pub fn or_visit_stmt_kind<'a, V: Visit<'a>>(visitor: &mut V, kind: &'a StmtKind) {
    match kind {
        StmtKind::Empty => {}
        StmtKind::Local(l) => visitor.visit_let(l),
        StmtKind::Item(i) => visitor.visit_item(i),
        StmtKind::Expr(e) => visitor.visit_expr(e),
    }
}

pub fn or_visit_expr_kind<'a, V: Visit<'a>>(visitor: &mut V, e: &'a ExprKind) {
    match e {
        ExprKind::Empty => {}
        ExprKind::Assign(a) => visitor.visit_assign(a),
        ExprKind::Modify(m) => visitor.visit_modify(m),
        ExprKind::Binary(b) => visitor.visit_binary(b),
        ExprKind::Unary(u) => visitor.visit_unary(u),
        ExprKind::Member(m) => visitor.visit_member(m),
        ExprKind::Index(i) => visitor.visit_index(i),
        ExprKind::Structor(s) => visitor.visit_structor(s),
        ExprKind::Path(p) => visitor.visit_path(p),
        ExprKind::Literal(l) => visitor.visit_literal(l),
        ExprKind::Array(a) => visitor.visit_array(a),
        ExprKind::ArrayRep(a) => visitor.visit_array_rep(a),
        ExprKind::AddrOf(a) => visitor.visit_addrof(a),
        ExprKind::Block(b) => visitor.visit_block(b),
        ExprKind::Group(g) => visitor.visit_group(g),
        ExprKind::Tuple(t) => visitor.visit_tuple(t),
        ExprKind::Loop(l) => visitor.visit_loop(l),
        ExprKind::While(w) => visitor.visit_while(w),
        ExprKind::If(i) => visitor.visit_if(i),
        ExprKind::For(f) => visitor.visit_for(f),
        ExprKind::Break(b) => visitor.visit_break(b),
        ExprKind::Return(r) => visitor.visit_return(r),
        ExprKind::Continue(c) => visitor.visit_continue(c),
    }
}
pub fn or_visit_member_kind<'a, V: Visit<'a>>(visitor: &mut V, kind: &'a MemberKind) {
    match kind {
        MemberKind::Call(field, args) => {
            visitor.visit_sym(field);
            visitor.visit_tuple(args);
        }
        MemberKind::Struct(field) => visitor.visit_sym(field),
        MemberKind::Tuple(field) => visitor.visit_literal(field),
    }
}