gfx/angle/checkout/src/compiler/translator/ReplaceVariable.cpp
author Noemi Erli <nerli@mozilla.com>
Thu, 24 May 2018 18:46:25 +0300
changeset 467844 ff8505d177b9
parent 451300 8ee92682ad1f
permissions -rw-r--r--
Backed out 3 changesets (bug 1459785) for causing https://bugzilla.mozilla.org/show_bug.cgi?id=1464089 a=backout Backed out changeset 88675b68241a (bug 1459785) Backed out changeset b7c91a6f1b0a (bug 1459785) Backed out changeset 21af8dc00aa8 (bug 1459785)

//
// Copyright (c) 2018 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// ReplaceVariable.cpp: Replace all references to a specific variable in the AST with references to
// another variable.

#include "compiler/translator/ReplaceVariable.h"

#include "compiler/translator/IntermNode.h"
#include "compiler/translator/IntermTraverse.h"

namespace sh
{

namespace
{

class ReplaceVariableTraverser : public TIntermTraverser
{
  public:
    ReplaceVariableTraverser(const TVariable *toBeReplaced, const TVariable *replacement)
        : TIntermTraverser(true, false, false),
          mToBeReplaced(toBeReplaced),
          mReplacement(replacement)
    {
    }

    void visitSymbol(TIntermSymbol *node) override
    {
        if (&node->variable() == mToBeReplaced)
        {
            queueReplacement(new TIntermSymbol(mReplacement), OriginalNode::IS_DROPPED);
        }
    }

  private:
    const TVariable *const mToBeReplaced;
    const TVariable *const mReplacement;
};

}  // anonymous namespace

// Replaces every occurrence of a variable with another variable.
void ReplaceVariable(TIntermBlock *root,
                     const TVariable *toBeReplaced,
                     const TVariable *replacement)
{
    ReplaceVariableTraverser traverser(toBeReplaced, replacement);
    root->traverse(&traverser);
    traverser.updateTree();
}

}  // namespace sh