1 //
2 // Copyright (c) 2015 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 // FindSymbol.cpp:
7 //     Utility for finding a symbol node inside an AST tree.
8 
9 #include "compiler/translator/FindSymbolNode.h"
10 
11 #include "compiler/translator/IntermTraverse.h"
12 
13 namespace sh
14 {
15 
16 namespace
17 {
18 
19 class SymbolFinder : public TIntermTraverser
20 {
21   public:
SymbolFinder(const TString & symbolName,TBasicType basicType)22     SymbolFinder(const TString &symbolName, TBasicType basicType)
23         : TIntermTraverser(true, false, false),
24           mSymbolName(symbolName),
25           mNodeFound(nullptr),
26           mBasicType(basicType)
27     {
28     }
29 
visitSymbol(TIntermSymbol * node)30     void visitSymbol(TIntermSymbol *node)
31     {
32         if (node->getBasicType() == mBasicType && node->getSymbol() == mSymbolName)
33         {
34             mNodeFound = node;
35         }
36     }
37 
isFound() const38     bool isFound() const { return mNodeFound != nullptr; }
getNode() const39     const TIntermSymbol *getNode() const { return mNodeFound; }
40 
41   private:
42     TString mSymbolName;
43     TIntermSymbol *mNodeFound;
44     TBasicType mBasicType;
45 };
46 
47 }  // anonymous namespace
48 
FindSymbolNode(TIntermNode * root,const TString & symbolName,TBasicType basicType)49 const TIntermSymbol *FindSymbolNode(TIntermNode *root,
50                                     const TString &symbolName,
51                                     TBasicType basicType)
52 {
53     SymbolFinder finder(symbolName, basicType);
54     root->traverse(&finder);
55     return finder.getNode();
56 }
57 
58 }  // namespace sh
59