Move validation from Intermediate::addUnaryMath to ParseContext

Intermediate should only have logic for creating node objects, validation
of parameter types belongs in ParseContext.

BUG=angleproject:952
TEST=angle_unittests, WebGL conformance tests

Change-Id: Ie90697641fabb2a837ccc4571a93616d63ea64e6
Reviewed-on: https://chromium-review.googlesource.com/262414
Tested-by: Olli Etuaho <oetuaho@nvidia.com>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Reviewed-by: Nicolas Capens <capn@chromium.org>
diff --git a/src/compiler/translator/Intermediate.cpp b/src/compiler/translator/Intermediate.cpp
index fd0038d..12fb5bd 100644
--- a/src/compiler/translator/Intermediate.cpp
+++ b/src/compiler/translator/Intermediate.cpp
@@ -127,52 +127,8 @@
 // Returns the added node.
 //
 TIntermTyped *TIntermediate::addUnaryMath(
-    TOperator op, TIntermNode *childNode, const TSourceLoc &line)
+    TOperator op, TIntermTyped *child, const TSourceLoc &line)
 {
-    TIntermUnary *node;
-    TIntermTyped *child = childNode->getAsTyped();
-
-    if (child == NULL)
-    {
-        mInfoSink.info.message(EPrefixInternalError, line,
-                               "Bad type in AddUnaryMath");
-        return NULL;
-    }
-
-    switch (op)
-    {
-      case EOpLogicalNot:
-        if (child->getBasicType() != EbtBool ||
-            child->isMatrix() ||
-            child->isArray() ||
-            child->isVector())
-        {
-            return NULL;
-        }
-        break;
-      case EOpBitwiseNot:
-        if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
-            child->isMatrix() ||
-            child->isArray())
-        {
-            return NULL;
-        }
-        break;
-      case EOpPostIncrement:
-      case EOpPreIncrement:
-      case EOpPostDecrement:
-      case EOpPreDecrement:
-      case EOpNegative:
-      case EOpPositive:
-        if (child->getBasicType() == EbtStruct ||
-            child->isArray())
-        {
-            return NULL;
-        }
-      default:
-        break;
-    }
-
     TIntermConstantUnion *childTempConstant = 0;
     if (child->getAsConstantUnion())
         childTempConstant = child->getAsConstantUnion();
@@ -180,7 +136,7 @@
     //
     // Make a new node for the operator.
     //
-    node = new TIntermUnary(op);
+    TIntermUnary *node = new TIntermUnary(op);
     node->setLine(line);
     node->setOperand(child);
 
diff --git a/src/compiler/translator/Intermediate.h b/src/compiler/translator/Intermediate.h
index b6c2455..30c959a 100644
--- a/src/compiler/translator/Intermediate.h
+++ b/src/compiler/translator/Intermediate.h
@@ -35,7 +35,7 @@
     TIntermTyped *addIndex(
         TOperator op, TIntermTyped *base, TIntermTyped *index, const TSourceLoc &);
     TIntermTyped *addUnaryMath(
-        TOperator op, TIntermNode *child, const TSourceLoc &);
+        TOperator op, TIntermTyped *child, const TSourceLoc &line);
     TIntermAggregate *growAggregate(
         TIntermNode *left, TIntermNode *right, const TSourceLoc &);
     TIntermAggregate *makeAggregate(TIntermNode *node, const TSourceLoc &);
diff --git a/src/compiler/translator/ParseContext.cpp b/src/compiler/translator/ParseContext.cpp
index 0e6ae31..77e70a7 100644
--- a/src/compiler/translator/ParseContext.cpp
+++ b/src/compiler/translator/ParseContext.cpp
@@ -2697,10 +2697,54 @@
     return node;
 }
 
+TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
+{
+    if (child == nullptr)
+    {
+        return nullptr;
+    }
+
+    switch (op)
+    {
+      case EOpLogicalNot:
+        if (child->getBasicType() != EbtBool ||
+            child->isMatrix() ||
+            child->isArray() ||
+            child->isVector())
+        {
+            return nullptr;
+        }
+        break;
+      case EOpBitwiseNot:
+        if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
+            child->isMatrix() ||
+            child->isArray())
+        {
+            return nullptr;
+        }
+        break;
+      case EOpPostIncrement:
+      case EOpPreIncrement:
+      case EOpPostDecrement:
+      case EOpPreDecrement:
+      case EOpNegative:
+      case EOpPositive:
+        if (child->getBasicType() == EbtStruct ||
+            child->isArray())
+        {
+            return nullptr;
+        }
+      default:
+        break;
+    }
+
+    return intermediate.addUnaryMath(op, child, loc);
+}
+
 TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
 {
-    TIntermTyped *node = intermediate.addUnaryMath(op, child, loc);
-    if (node == 0)
+    TIntermTyped *node = createUnaryMath(op, child, loc);
+    if (node == nullptr)
     {
         unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
         recover();
@@ -3048,7 +3092,7 @@
                     //
                     // Treat it like a built-in unary operator.
                     //
-                    callNode = intermediate.addUnaryMath(op, node, loc);
+                    callNode = createUnaryMath(op, node->getAsTyped(), loc);
                     if (callNode == nullptr)
                     {
                         std::stringstream extraInfoStream;
@@ -3063,12 +3107,12 @@
                     if (returnType.getBasicType() == EbtBool)
                     {
                         // Bool types should not have precision, so we'll override any precision
-                        // that might have been set by addUnaryMath.
+                        // that might have been set by createUnaryMath.
                         callNode->setType(returnType);
                     }
                     else
                     {
-                        // addUnaryMath has set the precision of the node based on the operand.
+                        // createUnaryMath has set the precision of the node based on the operand.
                         callNode->setTypePreservePrecision(returnType);
                     }
                 }
diff --git a/src/compiler/translator/ParseContext.h b/src/compiler/translator/ParseContext.h
index f32847e..fd1813e 100644
--- a/src/compiler/translator/ParseContext.h
+++ b/src/compiler/translator/ParseContext.h
@@ -191,6 +191,7 @@
         const TSourceLoc &loc);
     TIntermTyped *createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
         const TSourceLoc &loc);
+    TIntermTyped *createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc);
 
     // Return true if the checks pass
     bool binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right,