[cfe] Execute statements with StatementConstantEvaluator.

Altered the structure of the statement visitors now that ConstantEvaluator is an expression visitor. Currently uses the environment from the prior exprEvaluator, but will change in the future when needed.

No new const functionality, just set up for future ones.

Tests renamed so they actually run now.

Change-Id: Ic5914b0fc700aadeb0dcb28c08593f88e38582a2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/189220
Reviewed-by: Dmitry Stefantsov <dmitryas@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
diff --git a/pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart b/pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart
index 547cc29..a759519 100644
--- a/pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart
+++ b/pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart
@@ -989,6 +989,13 @@
     return result;
   }
 
+  /// Execute the statement using the [StatementConstantEvaluator].
+  Constant execute(Statement statement) {
+    StatementConstantEvaluator statementEvaluator =
+        new StatementConstantEvaluator(this);
+    return statement.accept(statementEvaluator);
+  }
+
   /// Create an error-constant indicating that an error has been detected during
   /// constant evaluation.
   AbortConstant createErrorConstant(TreeNode node, Message message,
@@ -2738,17 +2745,7 @@
         if (value is AbortConstant) return value;
         env.updateVariableValue(parameter, value);
       }
-      Statement body = function.body;
-      if (body is ReturnStatement) {
-        if (!enableConstFunctions) {
-          return createInvalidExpressionConstant(
-              node, "Return statements are not supported.");
-        }
-        return body.expression.accept(this);
-      } else {
-        return createInvalidExpressionConstant(
-            node, "Unsupported statement: ${body.runtimeType}.");
-      }
+      return execute(function.body);
     });
   }
 
@@ -3209,6 +3206,27 @@
   }
 }
 
+class StatementConstantEvaluator extends StatementVisitor<Constant> {
+  ConstantEvaluator exprEvaluator;
+
+  StatementConstantEvaluator(this.exprEvaluator) {
+    if (!exprEvaluator.enableConstFunctions) {
+      throw new UnsupportedError("Const functions feature is not enabled.");
+    }
+  }
+
+  /// Evaluate the expression using the [ConstantEvaluator].
+  Constant evaluate(Expression expr) => expr.accept(exprEvaluator);
+
+  @override
+  Constant defaultStatement(Statement node) => throw new UnsupportedError(
+      'Statement constant evaluation does not support ${node.runtimeType}.');
+
+  @override
+  Constant visitReturnStatement(ReturnStatement node) =>
+      evaluate(node.expression);
+}
+
 class ConstantCoverage {
   final Map<Uri, Set<Reference>> constructorCoverage;
 
diff --git a/pkg/front_end/test/spell_checking_list_code.txt b/pkg/front_end/test/spell_checking_list_code.txt
index 8fe1b6b..ba25ffb 100644
--- a/pkg/front_end/test/spell_checking_list_code.txt
+++ b/pkg/front_end/test/spell_checking_list_code.txt
@@ -389,6 +389,7 @@
 established
 estimate
 eval
+execute
 exhausted
 existence
 existentially
diff --git a/tests/language/const_functions/const_functions_disabled_simple_invocations.dart b/tests/language/const_functions/const_functions_disabled_simple_invocations_test.dart
similarity index 100%
rename from tests/language/const_functions/const_functions_disabled_simple_invocations.dart
rename to tests/language/const_functions/const_functions_disabled_simple_invocations_test.dart
diff --git a/tests/language/const_functions/const_functions_simple_invocations.dart b/tests/language/const_functions/const_functions_simple_invocations_test.dart
similarity index 100%
rename from tests/language/const_functions/const_functions_simple_invocations.dart
rename to tests/language/const_functions/const_functions_simple_invocations_test.dart