1 //
2 // Copyright (c) 2017 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 // RemoveEmptySwitchStatements.cpp: Remove switch statements that have an empty statement list.
7 
8 #include "compiler/translator/RemoveEmptySwitchStatements.h"
9 
10 #include "compiler/translator/IntermTraverse.h"
11 
12 namespace sh
13 {
14 
15 namespace
16 {
17 
18 class RemoveEmptySwitchStatementsTraverser : public TIntermTraverser
19 {
20   public:
RemoveEmptySwitchStatementsTraverser()21     RemoveEmptySwitchStatementsTraverser() : TIntermTraverser(true, false, false) {}
22 
23     bool visitSwitch(Visit visit, TIntermSwitch *node);
24 };
25 
visitSwitch(Visit visit,TIntermSwitch * node)26 bool RemoveEmptySwitchStatementsTraverser::visitSwitch(Visit visit, TIntermSwitch *node)
27 {
28     if (node->getStatementList()->getSequence()->empty())
29     {
30         // Just output the init statement.
31         if (node->getInit()->hasSideEffects())
32         {
33             queueReplacement(node->getInit(), OriginalNode::IS_DROPPED);
34         }
35         else
36         {
37             TIntermSequence emptyReplacement;
38             ASSERT(getParentNode()->getAsBlock());
39             mMultiReplacements.push_back(NodeReplaceWithMultipleEntry(getParentNode()->getAsBlock(),
40                                                                       node, emptyReplacement));
41         }
42         return false;  // Nothing inside the child nodes to traverse.
43     }
44     return true;
45 }
46 
47 }  // anonymous namespace
48 
RemoveEmptySwitchStatements(TIntermBlock * root)49 void RemoveEmptySwitchStatements(TIntermBlock *root)
50 {
51     RemoveEmptySwitchStatementsTraverser traverser;
52     root->traverse(&traverser);
53     traverser.updateTree();
54 }
55 
56 }  // namespace sh
57