Make sure that the AST root is always a sequence node

This enables inserting helper functions as an AST transformation.

BUG=angleproject:1116
TEST=angle_unittests

Change-Id: I169d4d3a726d0e389cb3444fe9dfb4c6c5d80155
Reviewed-on: https://chromium-review.googlesource.com/290513
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/Compiler.cpp b/src/compiler/translator/Compiler.cpp
index 990863a..6fac29a 100644
--- a/src/compiler/translator/Compiler.cpp
+++ b/src/compiler/translator/Compiler.cpp
@@ -242,7 +242,7 @@
         }
 
         root = parseContext.getTreeRoot();
-        success = intermediate.postProcess(root);
+        root = intermediate.postProcess(root);
 
         // Disallow expressions deemed too complex.
         if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
diff --git a/src/compiler/translator/Intermediate.cpp b/src/compiler/translator/Intermediate.cpp
index f753005..87bf2eb 100644
--- a/src/compiler/translator/Intermediate.cpp
+++ b/src/compiler/translator/Intermediate.cpp
@@ -426,19 +426,27 @@
 // This is to be executed once the final root is put on top by the parsing
 // process.
 //
-bool TIntermediate::postProcess(TIntermNode *root)
+TIntermAggregate *TIntermediate::postProcess(TIntermNode *root)
 {
-    if (root == NULL)
-        return true;
+    if (root == nullptr)
+        return nullptr;
 
     //
-    // First, finish off the top level sequence, if any
+    // Finish off the top level sequence, if any
     //
     TIntermAggregate *aggRoot = root->getAsAggregate();
-    if (aggRoot && aggRoot->getOp() == EOpNull)
+    if (aggRoot != nullptr && aggRoot->getOp() == EOpNull)
+    {
         aggRoot->setOp(EOpSequence);
+    }
+    else if (aggRoot == nullptr || aggRoot->getOp() != EOpSequence)
+    {
+        aggRoot = new TIntermAggregate(EOpSequence);
+        aggRoot->setLine(root->getLine());
+        aggRoot->getSequence()->push_back(root);
+    }
 
-    return true;
+    return aggRoot;
 }
 
 TIntermTyped *TIntermediate::foldAggregateBuiltIn(TIntermAggregate *aggregate)
diff --git a/src/compiler/translator/Intermediate.h b/src/compiler/translator/Intermediate.h
index e5c69d9..21d0b1d 100644
--- a/src/compiler/translator/Intermediate.h
+++ b/src/compiler/translator/Intermediate.h
@@ -60,7 +60,7 @@
     TIntermBranch *addBranch(TOperator, const TSourceLoc &);
     TIntermBranch *addBranch(TOperator, TIntermTyped *, const TSourceLoc &);
     TIntermTyped *addSwizzle(TVectorFields &, const TSourceLoc &);
-    bool postProcess(TIntermNode *);
+    TIntermAggregate *postProcess(TIntermNode *root);
 
     static void outputTree(TIntermNode *, TInfoSinkBase &);