1//
2// Copyright (c) 2018 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// ReplaceVariable.cpp: Replace all references to a specific variable in the AST with references to
7// another variable.
8
9#include "compiler/translator/tree_util/ReplaceVariable.h"
10
11#include "compiler/translator/IntermNode.h"
12#include "compiler/translator/tree_util/IntermTraverse.h"
13
14namespace sh
15{
16
17namespace
18{
19
20class ReplaceVariableTraverser : public TIntermTraverser
21{
22 public:
23 ReplaceVariableTraverser(const TVariable *toBeReplaced, const TIntermTyped *replacement)
24 : TIntermTraverser(true, false, false),
25 mToBeReplaced(toBeReplaced),
26 mReplacement(replacement)
27 {}
28
29 void visitSymbol(TIntermSymbol *node) override
30 {
31 if (&node->variable() == mToBeReplaced)
32 {
33 queueReplacement(mReplacement->deepCopy(), OriginalNode::IS_DROPPED);
34 }
35 }
36
37 private:
38 const TVariable *const mToBeReplaced;
39 const TIntermTyped *const mReplacement;
40};
41
42} // anonymous namespace
43
44// Replaces every occurrence of a variable with another variable.
45void ReplaceVariable(TIntermBlock *root,
46 const TVariable *toBeReplaced,
47 const TVariable *replacement)
48{
49 ReplaceVariableTraverser traverser(toBeReplaced, new TIntermSymbol(replacement));
50 root->traverse(&traverser);
51 traverser.updateTree();
52}
53
54// Replaces every occurrence of a variable with a TIntermNode.
55void ReplaceVariableWithTyped(TIntermBlock *root,
56 const TVariable *toBeReplaced,
57 const TIntermTyped *replacement)
58{
59 ReplaceVariableTraverser traverser(toBeReplaced, replacement);
60 root->traverse(&traverser);
61 traverser.updateTree();
62}
63
64} // namespace sh
65