Make false blocks produced by RewriteElseBlocks sequence nodes

All child nodes of selection should be sequence nodes, so that they will
output braces and extra braces can be removed from HLSL output.

Also make RewriteElseBlocks to reuse common IntermTraverser functionality
to simplify the code.

TEST=WebGL conformance tests on D3D9
BUG=angleproject:1013

Change-Id: Iafdc05468b22d110abcd020cf52c646dd98fb4a0
Reviewed-on: https://chromium-review.googlesource.com/273608
Tested-by: Olli Etuaho <oetuaho@nvidia.com>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Reviewed-by: Zhenyao Mo <zmo@chromium.org>
diff --git a/src/compiler/translator/RewriteElseBlocks.cpp b/src/compiler/translator/RewriteElseBlocks.cpp
index 4631b5d..ab27dfd 100644
--- a/src/compiler/translator/RewriteElseBlocks.cpp
+++ b/src/compiler/translator/RewriteElseBlocks.cpp
@@ -32,23 +32,6 @@
     TIntermNode *rewriteSelection(TIntermSelection *selection);
 };
 
-TIntermSymbol *MakeNewTemporary(const TString &name, TBasicType type)
-{
-    TType variableType(type, EbpHigh, EvqTemporary);
-    TIntermSymbol *node = new TIntermSymbol(-1, name, variableType);
-    node->setInternal(true);
-    return node;
-}
-
-TIntermBinary *MakeNewBinary(TOperator op, TIntermTyped *left, TIntermTyped *right, const TType &resultType)
-{
-    TIntermBinary *binary = new TIntermBinary(op);
-    binary->setLeft(left);
-    binary->setRight(right);
-    binary->setType(resultType);
-    return binary;
-}
-
 TIntermUnary *MakeNewUnary(TOperator op, TIntermTyped *operand)
 {
     TIntermUnary *unary = new TIntermUnary(op, operand->getType());
@@ -73,7 +56,7 @@
             {
                 TIntermNode *statement = (*node->getSequence())[statementIndex];
                 TIntermSelection *selection = statement->getAsSelectionNode();
-                if (selection && selection->getFalseBlock() != NULL)
+                if (selection && selection->getFalseBlock() != nullptr)
                 {
                     // Check for if / else if
                     TIntermSelection *elseIfBranch = selection->getFalseBlock()->getAsSelectionNode();
@@ -103,20 +86,20 @@
 
 TIntermNode *ElseBlockRewriter::rewriteSelection(TIntermSelection *selection)
 {
-    ASSERT(selection != NULL);
+    ASSERT(selection != nullptr);
 
-    TString temporaryName = "cond_" + str(mTemporaryIndex++);
+    nextTemporaryIndex();
+
     TIntermTyped *typedCondition = selection->getCondition()->getAsTyped();
-    TType resultType(EbtBool, EbpUndefined);
-    TIntermSymbol *conditionSymbolInit = MakeNewTemporary(temporaryName, EbtBool);
-    TIntermBinary *storeCondition = MakeNewBinary(EOpInitialize, conditionSymbolInit,
-                                                  typedCondition, resultType);
-    TIntermNode *negatedElse = NULL;
+    TIntermAggregate *storeCondition = createTempInitDeclaration(typedCondition);
 
-    TIntermSelection *falseBlock = NULL;
+    TIntermSelection *falseBlock = nullptr;
+
+    TType boolType(EbtBool, EbpUndefined, EvqTemporary);
 
     if (selection->getFalseBlock())
     {
+        TIntermAggregate *negatedElse = nullptr;
         // crbug.com/346463
         // D3D generates error messages claiming a function has no return value, when rewriting
         // an if-else clause that returns something non-void in a function. By appending dummy
@@ -126,24 +109,22 @@
             TString typeString = mFunctionType->getStruct() ? mFunctionType->getStruct()->name() :
                 mFunctionType->getBasicString();
             TString rawText = "return (" + typeString + ")0";
-            negatedElse = new TIntermRaw(*mFunctionType, rawText);
+            TIntermRaw *returnNode = new TIntermRaw(*mFunctionType, rawText);
+            negatedElse = new TIntermAggregate(EOpSequence);
+            negatedElse->getSequence()->push_back(returnNode);
         }
 
-        TIntermSymbol *conditionSymbolElse = MakeNewTemporary(temporaryName, EbtBool);
+        TIntermSymbol *conditionSymbolElse = createTempSymbol(boolType);
         TIntermUnary *negatedCondition = MakeNewUnary(EOpLogicalNot, conditionSymbolElse);
         falseBlock = new TIntermSelection(negatedCondition,
                                           selection->getFalseBlock(), negatedElse);
     }
 
-    TIntermSymbol *conditionSymbolSel = MakeNewTemporary(temporaryName, EbtBool);
-    TIntermSelection *newSelection = new TIntermSelection(conditionSymbolSel,
-                                                          selection->getTrueBlock(), falseBlock);
-
-    TIntermAggregate *declaration = new TIntermAggregate(EOpDeclaration);
-    declaration->getSequence()->push_back(storeCondition);
+    TIntermSymbol *conditionSymbolSel = createTempSymbol(boolType);
+    TIntermSelection *newSelection = new TIntermSelection(conditionSymbolSel, selection->getTrueBlock(), falseBlock);
 
     TIntermAggregate *block = new TIntermAggregate(EOpSequence);
-    block->getSequence()->push_back(declaration);
+    block->getSequence()->push_back(storeCondition);
     block->getSequence()->push_back(newSelection);
 
     return block;
@@ -151,9 +132,10 @@
 
 }
 
-void RewriteElseBlocks(TIntermNode *node)
+void RewriteElseBlocks(TIntermNode *node, unsigned int *temporaryIndex)
 {
     ElseBlockRewriter rewriter;
+    rewriter.useTemporaryIndex(temporaryIndex);
     node->traverse(&rewriter);
 }