1//
2// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6// NodeSearch.h: Utilities for searching translator node graphs
7//
8
9#ifndef TRANSLATOR_NODESEARCH_H_
10#define TRANSLATOR_NODESEARCH_H_
11
12#include "compiler/translator/IntermNode.h"
13
14namespace sh
15{
16
17template <class Parent>
18class NodeSearchTraverser : public TIntermTraverser
19{
20  public:
21    NodeSearchTraverser()
22        : mFound(false)
23    {}
24
25    bool found() const { return mFound; }
26
27    static bool search(TIntermNode *node)
28    {
29        Parent searchTraverser;
30        node->traverse(&searchTraverser);
31        return searchTraverser.found();
32    }
33
34  protected:
35    bool mFound;
36};
37
38class FindDiscard : public NodeSearchTraverser<FindDiscard>
39{
40  public:
41    virtual bool visitBranch(Visit visit, TIntermBranch *node)
42    {
43        switch (node->getFlowOp())
44        {
45          case EOpKill:
46            mFound = true;
47            break;
48
49          default: break;
50        }
51
52        return !mFound;
53    }
54};
55
56class FindSideEffectRewriting : public NodeSearchTraverser<FindSideEffectRewriting>
57{
58  public:
59    virtual bool visitBinary(Visit visit, TIntermBinary *node)
60    {
61        switch (node->getOp())
62        {
63          case EOpLogicalOr:
64          case EOpLogicalAnd:
65            if (node->getRight()->hasSideEffects())
66            {
67                mFound = true;
68            }
69            break;
70
71          default: break;
72        }
73
74        return !mFound;
75    }
76};
77
78}
79
80#endif // TRANSLATOR_NODESEARCH_H_
81