aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.Shader/StructuredIr/AstBlockVisitor.cs
blob: 16efeff727f5d41663cccd5ddfcaf866659687ef (plain) (blame)
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
using System;
using System.Collections.Generic;

using static Ryujinx.Graphics.Shader.StructuredIr.AstHelper;

namespace Ryujinx.Graphics.Shader.StructuredIr
{
    class AstBlockVisitor
    {
        public AstBlock Block { get; private set; }

        public class BlockVisitationEventArgs : EventArgs
        {
            public AstBlock Block { get; }

            public BlockVisitationEventArgs(AstBlock block)
            {
                Block = block;
            }
        }

        public event EventHandler<BlockVisitationEventArgs> BlockEntered;
        public event EventHandler<BlockVisitationEventArgs> BlockLeft;

        public AstBlockVisitor(AstBlock mainBlock)
        {
            Block = mainBlock;
        }

        public IEnumerable<IAstNode> Visit()
        {
            IAstNode node = Block.First;

            while (node != null)
            {
                // We reached a child block, visit the nodes inside.
                while (node is AstBlock childBlock)
                {
                    Block = childBlock;

                    node = childBlock.First;

                    BlockEntered?.Invoke(this, new BlockVisitationEventArgs(Block));
                }

                // Node may be null, if the block is empty.
                if (node != null)
                {
                    IAstNode next = Next(node);

                    yield return node;

                    node = next;
                }

                // We reached the end of the list, go up on tree to the parent blocks.
                while (node == null && Block.Type != AstBlockType.Main)
                {
                    BlockLeft?.Invoke(this, new BlockVisitationEventArgs(Block));

                    node = Next(Block);

                    Block = Block.Parent;
                }
            }
        }
    }
}