midenc-hir 0.8.1

High-level Intermediate Representation for Miden Assembly
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
use super::WalkResult;
use crate::{
    Block, BlockRef, Direction, Operation, OperationRef, Region, RegionRef,
    UnsafeIntrusiveEntityRef,
};

/// The traversal order for a walk of a region, block, or operation
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum WalkOrder {
    PreOrder,
    PostOrder,
}

/// Encodes the current walk stage for generic walkers.
///
/// When walking an operation, we can either choose a pre- or post-traversal walker which invokes
/// the callback on an operation before/after all its attached regions have been visited, or choose
/// a generic walker where the callback is invoked on the operation N+1 times, where N is the number
/// of regions attached to that operation. [WalkStage] encodes the current stage of the walk, i.e.
/// which regions have already been visited, and the callback accepts an additional argument for
/// the current stage. Such generic walkers that accept stage-aware callbacks are only applicable
/// when the callback operations on an operation (i.e. doesn't apply to callbacks on blocks or
/// regions).
#[derive(Clone, PartialEq, Eq)]
pub struct WalkStage {
    /// The number of regions in the operation
    num_regions: usize,
    /// The next region to visit in the operation
    next_region: Option<RegionRef>,
}
impl WalkStage {
    pub fn new(op: OperationRef) -> Self {
        let op = op.borrow();
        Self {
            num_regions: op.num_regions(),
            next_region: op.regions().front().as_pointer(),
        }
    }

    /// Returns true if the parent operation is being visited before all regions.
    #[inline]
    pub fn is_before_all_regions(&self) -> bool {
        self.next_region.is_some_and(|r| r.prev().is_none())
    }

    /// Returns true if the parent operation is being visited just before visiting `region`
    #[inline]
    pub fn is_before_region(&self, region: RegionRef) -> bool {
        self.next_region.is_some_and(|r| r.next().is_some_and(|next| next == region))
    }

    /// Returns true if the parent operation is being visited just after visiting `region`
    #[inline]
    pub fn is_after_region(&self, region: RegionRef) -> bool {
        self.next_region.is_some_and(|r| r.prev().is_some_and(|prev| prev == region))
    }

    /// Returns true if the parent operation is being visited after all regions.
    #[inline]
    pub fn is_after_all_regions(&self) -> bool {
        self.next_region.is_none()
    }

    /// Advance the walk stage
    #[inline]
    pub fn advance(&mut self) {
        if let Some(next_region) = self.next_region.take() {
            self.next_region = next_region.next();
        }
    }

    /// Returns the next region that will be visited
    #[inline(always)]
    pub const fn next_region(&self) -> Option<RegionRef> {
        self.next_region
    }
}

pub trait WalkDirection =
    Walker<Region, BlockRef> + Walker<Block, OperationRef> + Walker<Operation, RegionRef>;

/// [Walk] represents an implementation of a depth-first traversal (pre- or post-order) from some
/// root object in the entity graph, to children of a given entity type.
///
/// An implementation of this trait specifies a type, `T`, corresponding to the type of item being
/// walked, while `Self` is the root entity, possibly of the same type, which may contain `T`. Thus
/// traversing from the root to all of the leaves, we will visit all reachable `T` nested within
/// `Self`, possibly including itself.
///
/// In cases where the root entity and the entity type being visited are the same, callbacks given
/// to this trait's methods are invoked on both the root entity and any children of that type. This
/// would require re-borrowing the root entity, so to distinguish between immutable and mutable
/// visits, this trait has a mutable variant, [WalkMut], which ensures that the root entity is not
/// borrowed during the traversal, and thus can be mutably borrowed by the visitor if needed.
pub trait Walk<T> {
    /// Walk all `T` in `self` in a specific order, applying the given callback to each.
    ///
    /// This is very similar to [Walk::walk], except the callback has no control over the traversal,
    /// and must be infallible.
    fn walk_all<D, F>(&self, order: WalkOrder, mut callback: F)
    where
        D: WalkDirection,
        F: FnMut(&T),
    {
        let _ = self.walk::<D, _, _>(order, |t| {
            callback(t);

            WalkResult::<()>::Continue(())
        });
    }

    /// Walk all `T` in `self` using a pre-order, depth-first traversal, applying the given callback
    /// to each `T`.
    #[inline]
    fn prewalk_all<D, F>(&self, callback: F)
    where
        D: WalkDirection,
        F: FnMut(&T),
    {
        self.walk_all::<D, _>(WalkOrder::PreOrder, callback)
    }

    /// Walk all `T` in `self` using a post-order, depth-first traversal, applying the given callback
    /// to each `T`.
    #[inline]
    fn postwalk_all<D, F>(&self, callback: F)
    where
        D: WalkDirection,
        F: FnMut(&T),
    {
        self.walk_all::<D, _>(WalkOrder::PostOrder, callback)
    }

    /// Walk `self` in the given order, visiting each `T` and applying the given callback to them.
    ///
    /// The given callback can control the traversal using the [WalkResult] it returns:
    ///
    /// * `WalkResult::Skip` will skip the walk of the current item and its nested elements that
    ///   have not been visited already, continuing with the next item.
    /// * `WalkResult::Break` will interrupt the walk, and no more items will be visited
    /// * `WalkResult::Continue` will continue the walk
    fn walk<D, F, B>(&self, order: WalkOrder, callback: F) -> WalkResult<B>
    where
        D: WalkDirection,
        F: FnMut(&T) -> WalkResult<B>;

    /// Walk all `T` in `self` using a pre-order, depth-first traversal, applying the given callback
    /// to each `T`, and determining how to proceed based on the returned [WalkResult].
    #[inline]
    fn prewalk<D, F, B>(&self, callback: F) -> WalkResult<B>
    where
        D: WalkDirection,
        F: FnMut(&T) -> WalkResult<B>,
    {
        self.walk::<D, _, _>(WalkOrder::PreOrder, callback)
    }

    /// Walk all `T` in `self` using a post-order, depth-first traversal, applying the given callback
    /// to each `T`, and determining how to proceed based on the returned [WalkResult].
    #[inline]
    fn postwalk<D, F, B>(&self, callback: F) -> WalkResult<B>
    where
        D: WalkDirection,
        F: FnMut(&T) -> WalkResult<B>,
    {
        self.walk::<D, _, _>(WalkOrder::PostOrder, callback)
    }
}

/// A mutable variant of [Walk], for traversal which may mutate visited entities.
pub trait WalkMut<T> {
    /// Walk all `T` in `self` in a specific order, applying the given callback to each.
    ///
    /// This is very similar to [WalkMut::walk_mut], except the callback has no control over the
    /// traversal, and must be infallible.
    fn walk_all_mut<D, F>(&mut self, order: WalkOrder, mut callback: F)
    where
        D: WalkDirection,
        F: FnMut(&mut T),
    {
        let _ = self.walk_mut::<D, _, _>(order, |t| {
            callback(t);

            WalkResult::<()>::Continue(())
        });
    }

    /// Walk all `T` in `self` using a pre-order, depth-first traversal, applying the given callback
    /// to each `T`.
    #[inline]
    fn prewalk_all_mut<D, F>(&mut self, callback: F)
    where
        D: WalkDirection,
        F: FnMut(&mut T),
    {
        self.walk_all_mut::<D, _>(WalkOrder::PreOrder, callback)
    }

    /// Walk all `T` in `self` using a post-order, depth-first traversal, applying the given callback
    /// to each `T`.
    #[inline]
    fn postwalk_all_mut<D, F>(&mut self, callback: F)
    where
        D: WalkDirection,
        F: FnMut(&mut T),
    {
        self.walk_all_mut::<D, _>(WalkOrder::PostOrder, callback)
    }

    /// Walk `self` in the given order, visiting each `T` and applying the given callback to them.
    ///
    /// The given callback can control the traversal using the [WalkResult] it returns:
    ///
    /// * `WalkResult::Skip` will skip the walk of the current item and its nested elements that
    ///   have not been visited already, continuing with the next item.
    /// * `WalkResult::Break` will interrupt the walk, and no more items will be visited
    /// * `WalkResult::Continue` will continue the walk
    fn walk_mut<D, F, B>(&mut self, order: WalkOrder, callback: F) -> WalkResult<B>
    where
        D: WalkDirection,
        F: FnMut(&mut T) -> WalkResult<B>;

    /// Walk all `T` in `self` using a pre-order, depth-first traversal, applying the given callback
    /// to each `T`, and determining how to proceed based on the returned [WalkResult].
    #[inline]
    fn prewalk_mut_interruptible<D, F, B>(&mut self, callback: F) -> WalkResult<B>
    where
        D: WalkDirection,
        F: FnMut(&mut T) -> WalkResult<B>,
    {
        self.walk_mut::<D, _, _>(WalkOrder::PreOrder, callback)
    }

    /// Walk all `T` in `self` using a post-order, depth-first traversal, applying the given callback
    /// to each `T`, and determining how to proceed based on the returned [WalkResult].
    #[inline]
    fn postwalk_mut_interruptible<D, F, B>(&mut self, callback: F) -> WalkResult<B>
    where
        D: WalkDirection,
        F: FnMut(&mut T) -> WalkResult<B>,
    {
        self.walk_mut::<D, _, _>(WalkOrder::PostOrder, callback)
    }
}

/// [RawWalk] is a variation of [Walk]/[WalkMut] that performs the traversal while ensuring that
/// no entity is borrowed when visitor callbacks are invoked. This allows the visitor to freely
/// obtain mutable/immutable borrows without having to worry if the traversal is holding a borrow
/// somewhere.
pub trait RawWalk<T> {
    /// Walk all `T` in `self` in a specific order, applying the given callback to each.
    ///
    /// This is very similar to [RawWalk::raw_walk], except the callback has no control
    /// over the traversal, and must be infallible.
    fn raw_walk_all<D, F>(&self, order: WalkOrder, mut callback: F)
    where
        D: WalkDirection,
        F: FnMut(UnsafeIntrusiveEntityRef<T>),
    {
        let _ = self.raw_walk::<D, _, _>(order, |t| {
            callback(t);

            WalkResult::<()>::Continue(())
        });
    }

    /// Walk all `T` in `self` using a pre-order, depth-first traversal, applying the given callback
    /// to each `T`.
    #[inline]
    fn raw_prewalk_all<D, F>(&self, callback: F)
    where
        D: WalkDirection,
        F: FnMut(UnsafeIntrusiveEntityRef<T>),
    {
        self.raw_walk_all::<D, _>(WalkOrder::PreOrder, callback)
    }

    /// Walk all `T` in `self` using a post-order, depth-first traversal, applying the given callback
    /// to each `T`.
    #[inline]
    fn raw_postwalk_all<D, F>(&self, callback: F)
    where
        D: WalkDirection,
        F: FnMut(UnsafeIntrusiveEntityRef<T>),
    {
        self.raw_walk_all::<D, _>(WalkOrder::PostOrder, callback)
    }

    /// Walk `self` in the given order, visiting each `T` and applying the given callback to them.
    ///
    /// The given callback can control the traversal using the [WalkResult] it returns:
    ///
    /// * `WalkResult::Skip` will skip the walk of the current item and its nested elements that
    ///   have not been visited already, continuing with the next item.
    /// * `WalkResult::Break` will interrupt the walk, and no more items will be visited
    /// * `WalkResult::Continue` will continue the walk
    fn raw_walk<D, F, B>(&self, order: WalkOrder, callback: F) -> WalkResult<B>
    where
        D: WalkDirection,
        F: FnMut(UnsafeIntrusiveEntityRef<T>) -> WalkResult<B>;

    /// Walk all `T` in `self` using a pre-order, depth-first traversal, applying the given callback
    /// to each `T`, and determining how to proceed based on the returned [WalkResult].
    #[inline]
    fn raw_prewalk<D, F, B>(&self, callback: F) -> WalkResult<B>
    where
        D: WalkDirection,
        F: FnMut(UnsafeIntrusiveEntityRef<T>) -> WalkResult<B>,
    {
        self.raw_walk::<D, _, _>(WalkOrder::PreOrder, callback)
    }

    /// Walk all `T` in `self` using a post-order, depth-first traversal, applying the given callback
    /// to each `T`, and determining how to proceed based on the returned [WalkResult].
    #[inline]
    fn raw_postwalk<D, F, B>(&self, callback: F) -> WalkResult<B>
    where
        D: WalkDirection,
        F: FnMut(UnsafeIntrusiveEntityRef<T>) -> WalkResult<B>,
    {
        self.raw_walk::<D, _, _>(WalkOrder::PostOrder, callback)
    }
}

/// Walking operations nested within an [Operation], including itself
impl RawWalk<Operation> for OperationRef {
    fn raw_walk<D, F, B>(&self, order: WalkOrder, mut callback: F) -> WalkResult<B>
    where
        D: WalkDirection,
        F: FnMut(UnsafeIntrusiveEntityRef<Operation>) -> WalkResult<B>,
    {
        raw_walk_operations::<D, _, _>(*self, order, &mut callback)
    }
}

impl Walk<Operation> for OperationRef {
    fn walk<D, F, B>(&self, order: WalkOrder, mut callback: F) -> WalkResult<B>
    where
        D: WalkDirection,
        F: FnMut(&Operation) -> WalkResult<B>,
    {
        let mut wrapper = |op: OperationRef| callback(&op.borrow());
        raw_walk_operations::<D, _, _>(*self, order, &mut wrapper)
    }
}

impl WalkMut<Operation> for OperationRef {
    fn walk_mut<D, F, B>(&mut self, order: WalkOrder, mut callback: F) -> WalkResult<B>
    where
        D: WalkDirection,
        F: FnMut(&mut Operation) -> WalkResult<B>,
    {
        let mut wrapper = |mut op: OperationRef| callback(&mut op.borrow_mut());
        raw_walk_operations::<D, _, _>(*self, order, &mut wrapper)
    }
}

impl Walk<Operation> for Operation {
    fn walk<D, F, B>(&self, order: WalkOrder, mut callback: F) -> WalkResult<B>
    where
        D: WalkDirection,
        F: FnMut(&Operation) -> WalkResult<B>,
    {
        let mut wrapper = |op: OperationRef| callback(&op.borrow());
        raw_walk_operations::<D, _, _>(self.as_operation_ref(), order, &mut wrapper)
    }
}

/// Walking regions of an [Operation], and those of all nested operations
impl RawWalk<Region> for OperationRef {
    fn raw_walk<D, F, B>(&self, order: WalkOrder, mut callback: F) -> WalkResult<B>
    where
        D: WalkDirection,
        F: FnMut(UnsafeIntrusiveEntityRef<Region>) -> WalkResult<B>,
    {
        raw_walk_regions::<D, _, _>(*self, order, &mut callback)
    }
}

impl Walk<Region> for OperationRef {
    fn walk<D, F, B>(&self, order: WalkOrder, mut callback: F) -> WalkResult<B>
    where
        D: WalkDirection,
        F: FnMut(&Region) -> WalkResult<B>,
    {
        let mut wrapper = |region: RegionRef| callback(&region.borrow());
        raw_walk_regions::<D, _, _>(*self, order, &mut wrapper)
    }
}

impl WalkMut<Region> for OperationRef {
    fn walk_mut<D, F, B>(&mut self, order: WalkOrder, mut callback: F) -> WalkResult<B>
    where
        D: WalkDirection,
        F: FnMut(&mut Region) -> WalkResult<B>,
    {
        let mut wrapper = |mut region: RegionRef| callback(&mut region.borrow_mut());
        raw_walk_regions::<D, _, _>(*self, order, &mut wrapper)
    }
}

impl Walk<Region> for Operation {
    fn walk<D, F, B>(&self, order: WalkOrder, mut callback: F) -> WalkResult<B>
    where
        D: WalkDirection,
        F: FnMut(&Region) -> WalkResult<B>,
    {
        let mut wrapper = |region: RegionRef| callback(&region.borrow());
        raw_walk_regions::<D, _, _>(self.as_operation_ref(), order, &mut wrapper)
    }
}

#[allow(unused)]
pub fn raw_walk<D, F, B>(op: OperationRef, callback: &mut F) -> WalkResult<B>
where
    D: WalkDirection,
    F: FnMut(OperationRef, &WalkStage) -> WalkResult<B>,
{
    let mut stage = WalkStage::new(op);

    let mut next_region = stage.next_region();
    while let Some(region) = next_region.take() {
        // Invoke callback on the parent op before visiting each child region
        let result = callback(op, &stage);

        match result {
            WalkResult::Skip => return WalkResult::Continue(()),
            err @ WalkResult::Break(_) => return err,
            WalkResult::Continue(_) => {
                stage.advance();

                let mut next_block = D::start(&*region.borrow());
                while let Some(block) = next_block.take() {
                    next_block = D::continue_walk(block);

                    let mut next_op = D::start(&*block.borrow());
                    while let Some(op) = next_op.take() {
                        next_op = D::continue_walk(op);

                        raw_walk::<D, _, _>(op, callback)?;
                    }
                }
            }
        }
    }

    // Invoke callback after all regions have been visited
    callback(op, &stage)
}

fn raw_walk_regions<D, F, B>(op: OperationRef, order: WalkOrder, callback: &mut F) -> WalkResult<B>
where
    D: WalkDirection,
    F: FnMut(RegionRef) -> WalkResult<B>,
{
    let mut next_region = D::start(&*op.borrow());
    while let Some(region) = next_region.take() {
        next_region = D::continue_walk(region);

        if matches!(order, WalkOrder::PreOrder) {
            let result = callback(region);
            match result {
                WalkResult::Skip => continue,
                err @ WalkResult::Break(_) => return err,
                _ => (),
            }
        }

        let mut next_block = D::start(&*region.borrow());
        while let Some(block) = next_block.take() {
            next_block = D::continue_walk(block);

            let mut next_op = D::start(&*block.borrow());
            while let Some(op) = next_op.take() {
                next_op = D::continue_walk(op);

                raw_walk_regions::<D, _, _>(op, order, callback)?;
            }
        }

        if matches!(order, WalkOrder::PostOrder) {
            callback(region)?;
        }
    }

    WalkResult::Continue(())
}

#[allow(unused)]
fn raw_walk_blocks<D, F, B>(op: OperationRef, order: WalkOrder, callback: &mut F) -> WalkResult<B>
where
    D: WalkDirection,
    F: FnMut(BlockRef) -> WalkResult<B>,
{
    let mut next_region = D::start(&*op.borrow());
    while let Some(region) = next_region.take() {
        next_region = D::continue_walk(region);

        let mut next_block = D::start(&*region.borrow());
        while let Some(block) = next_block.take() {
            next_block = D::continue_walk(block);

            if matches!(order, WalkOrder::PreOrder) {
                let result = callback(block);
                match result {
                    WalkResult::Skip => continue,
                    err @ WalkResult::Break(_) => return err,
                    _ => (),
                }
            }

            let mut next_op = D::start(&*block.borrow());
            while let Some(op) = next_op.take() {
                next_op = D::continue_walk(op);

                raw_walk_blocks::<D, _, _>(op, order, callback)?;
            }

            if matches!(order, WalkOrder::PostOrder) {
                callback(block)?;
            }
        }
    }

    WalkResult::Continue(())
}

fn raw_walk_operations<D, F, B>(
    op: OperationRef,
    order: WalkOrder,
    callback: &mut F,
) -> WalkResult<B>
where
    D: WalkDirection,
    F: FnMut(OperationRef) -> WalkResult<B>,
{
    if matches!(order, WalkOrder::PreOrder) {
        let result = callback(op);
        match result {
            WalkResult::Skip => return WalkResult::Continue(()),
            err @ WalkResult::Break(_) => return err,
            _ => (),
        }
    }

    let mut next_region = D::start(&*op.borrow());
    while let Some(region) = next_region.take() {
        next_region = D::continue_walk(region);

        let mut next_block = D::start(&*region.borrow());
        while let Some(block) = next_block.take() {
            next_block = D::continue_walk(block);

            let mut next_op = D::start(&*block.borrow());
            while let Some(op) = next_op.take() {
                next_op = D::continue_walk(op);

                raw_walk_operations::<D, _, _>(op, order, callback)?;
            }
        }
    }

    if matches!(order, WalkOrder::PostOrder) {
        callback(op)?;
    }

    WalkResult::Continue(())
}

fn raw_walk_region_operations<D, F, B>(
    region: RegionRef,
    order: WalkOrder,
    callback: &mut F,
) -> WalkResult<B>
where
    D: WalkDirection,
    F: FnMut(OperationRef) -> WalkResult<B>,
{
    let mut next_block = D::start(&*region.borrow());
    while let Some(block) = next_block.take() {
        next_block = D::continue_walk(block);

        let mut next_op = D::start(&*block.borrow());
        while let Some(op) = next_op.take() {
            next_op = D::continue_walk(op);

            raw_walk_operations::<D, _, _>(op, order, callback)?;
        }
    }

    WalkResult::Continue(())
}

/// Walking operations nested within a [Region]
impl RawWalk<Operation> for RegionRef {
    fn raw_walk<D, F, B>(&self, order: WalkOrder, mut callback: F) -> WalkResult<B>
    where
        D: WalkDirection,
        F: FnMut(UnsafeIntrusiveEntityRef<Operation>) -> WalkResult<B>,
    {
        raw_walk_region_operations::<D, _, _>(*self, order, &mut callback)
    }
}

impl Walk<Operation> for RegionRef {
    fn walk<D, F, B>(&self, order: WalkOrder, mut callback: F) -> WalkResult<B>
    where
        D: WalkDirection,
        F: FnMut(&Operation) -> WalkResult<B>,
    {
        let mut wrapper = |op: OperationRef| callback(&op.borrow());
        raw_walk_region_operations::<D, _, _>(*self, order, &mut wrapper)
    }
}

impl WalkMut<Operation> for RegionRef {
    fn walk_mut<D, F, B>(&mut self, order: WalkOrder, mut callback: F) -> WalkResult<B>
    where
        D: WalkDirection,
        F: FnMut(&mut Operation) -> WalkResult<B>,
    {
        let mut wrapper = |mut op: OperationRef| callback(&mut op.borrow_mut());
        raw_walk_region_operations::<D, _, _>(*self, order, &mut wrapper)
    }
}

impl Walk<Operation> for Region {
    fn walk<D, F, B>(&self, order: WalkOrder, mut callback: F) -> WalkResult<B>
    where
        D: WalkDirection,
        F: FnMut(&Operation) -> WalkResult<B>,
    {
        let mut wrapper = |op: OperationRef| callback(&op.borrow());
        raw_walk_region_operations::<D, _, _>(self.as_region_ref(), order, &mut wrapper)
    }
}

pub trait Walker<Parent, Child> {
    fn start(entity: &Parent) -> Option<Child>;
    fn continue_walk(child: Child) -> Option<Child>;
}

/// A custom `Walker` that is the same as [crate::Forward], except the operations of each block are
/// visited bottom-up, i.e. as if [crate::Backward] applied just to [crate::Block].
pub struct ReverseBlock;

impl Walker<Region, BlockRef> for ReverseBlock {
    #[inline(always)]
    fn start(entity: &Region) -> Option<BlockRef> {
        entity.body().front().as_pointer()
    }

    #[inline(always)]
    fn continue_walk(child: BlockRef) -> Option<BlockRef> {
        child.next()
    }
}

impl Walker<Block, OperationRef> for ReverseBlock {
    #[inline(always)]
    fn start(entity: &Block) -> Option<OperationRef> {
        entity.body().back().as_pointer()
    }

    #[inline(always)]
    fn continue_walk(child: OperationRef) -> Option<OperationRef> {
        child.prev()
    }
}

impl Walker<Operation, RegionRef> for ReverseBlock {
    #[inline(always)]
    fn start(entity: &Operation) -> Option<RegionRef> {
        entity.regions().front().as_pointer()
    }

    #[inline(always)]
    fn continue_walk(child: RegionRef) -> Option<RegionRef> {
        child.next()
    }
}

impl<D: Direction> Walker<Region, BlockRef> for D {
    #[inline(always)]
    fn start(entity: &Region) -> Option<BlockRef> {
        if const { D::IS_FORWARD } {
            entity.body().front().as_pointer()
        } else {
            entity.body().back().as_pointer()
        }
    }

    #[inline(always)]
    fn continue_walk(child: BlockRef) -> Option<BlockRef> {
        if const { D::IS_FORWARD } {
            child.next()
        } else {
            child.prev()
        }
    }
}

impl<D: Direction> Walker<Block, OperationRef> for D {
    #[inline(always)]
    fn start(entity: &Block) -> Option<OperationRef> {
        if const { D::IS_FORWARD } {
            entity.body().front().as_pointer()
        } else {
            entity.body().back().as_pointer()
        }
    }

    #[inline(always)]
    fn continue_walk(child: OperationRef) -> Option<OperationRef> {
        if const { D::IS_FORWARD } {
            child.next()
        } else {
            child.prev()
        }
    }
}

impl<D: Direction> Walker<Operation, RegionRef> for D {
    #[inline(always)]
    fn start(entity: &Operation) -> Option<RegionRef> {
        if const { D::IS_FORWARD } {
            entity.regions().front().as_pointer()
        } else {
            entity.regions().back().as_pointer()
        }
    }

    #[inline(always)]
    fn continue_walk(child: RegionRef) -> Option<RegionRef> {
        if const { D::IS_FORWARD } {
            child.next()
        } else {
            child.prev()
        }
    }
}