1 // 2 // Copyright 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/tree_util/FindSymbolNode.h" 10 11 #include "compiler/translator/ImmutableString.h" 12 #include "compiler/translator/Symbol.h" 13 #include "compiler/translator/tree_util/IntermTraverse.h" 14 15 namespace sh 16 { 17 18 namespace 19 { 20 21 class SymbolFinder : public TIntermTraverser 22 { 23 public: SymbolFinder(const ImmutableString & symbolName)24 SymbolFinder(const ImmutableString &symbolName) 25 : TIntermTraverser(true, false, false), mSymbolName(symbolName), mNodeFound(nullptr) 26 {} 27 visitSymbol(TIntermSymbol * node)28 void visitSymbol(TIntermSymbol *node) 29 { 30 if (node->variable().symbolType() != SymbolType::Empty && node->getName() == mSymbolName) 31 { 32 mNodeFound = node; 33 } 34 } 35 isFound() const36 bool isFound() const { return mNodeFound != nullptr; } getNode() const37 const TIntermSymbol *getNode() const { return mNodeFound; } 38 39 private: 40 ImmutableString mSymbolName; 41 TIntermSymbol *mNodeFound; 42 }; 43 44 } // anonymous namespace 45 FindSymbolNode(TIntermNode * root,const ImmutableString & symbolName)46const TIntermSymbol *FindSymbolNode(TIntermNode *root, const ImmutableString &symbolName) 47 { 48 SymbolFinder finder(symbolName); 49 root->traverse(&finder); 50 return finder.getNode(); 51 } 52 53 } // namespace sh 54