Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(22)

Unified Diff: lib/src/codegen/side_effect_analysis.dart

Issue 1133593004: fixes #131, use before define from variables to classes (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/src/codegen/js_names.dart ('k') | lib/src/dependency_graph.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/codegen/side_effect_analysis.dart
diff --git a/lib/src/codegen/side_effect_analysis.dart b/lib/src/codegen/side_effect_analysis.dart
index cdf8e7939a358a1f9b5c7bb2d7f11865e79376a5..6ae46d83f4fcf0edd531288ecfa359321036ccfa 100644
--- a/lib/src/codegen/side_effect_analysis.dart
+++ b/lib/src/codegen/side_effect_analysis.dart
@@ -5,7 +5,11 @@
library dev_compiler.src.codegen.side_effect_analysis;
import 'package:analyzer/src/generated/ast.dart';
+import 'package:analyzer/src/generated/constant.dart';
import 'package:analyzer/src/generated/element.dart';
+import 'package:analyzer/src/generated/error.dart' show ErrorReporter;
+import 'package:analyzer/src/generated/engine.dart' show RecordingErrorListener;
+import 'package:analyzer/src/generated/resolver.dart' show TypeProvider;
/// True is the expression can be evaluated multiple times without causing
/// code execution. This is true for final fields. This can be true for local
@@ -86,3 +90,34 @@ class _AssignmentFinder extends RecursiveAstVisitor {
}
}
}
+
+class ConstFieldVisitor {
+ final ConstantVisitor _constantVisitor;
+
+ ConstFieldVisitor(TypeProvider types, CompilationUnit unit)
+ : _constantVisitor = new ConstantVisitor.con1(types,
+ new ErrorReporter(new RecordingErrorListener(), unit.element.source));
+
+ // TODO(jmesserly): this is used to determine if the field initialization is
+ // side effect free. We should make the check more general, as things like
+ // list/map literals/regexp are also side effect free and fairly common
+ // to use as field initializers.
+ bool isFieldInitConstant(VariableDeclaration field) =>
+ field.initializer == null || computeConstant(field) != null;
+
+ DartObjectImpl computeConstant(VariableDeclaration field) {
+ // If the constant is already computed by ConstantEvaluator, just return it.
+ VariableElementImpl element = field.element;
+ var result = element.evaluationResult;
+ if (result != null) return result.value;
+
+ // ConstantEvaluator will not compute constants for non-const fields,
+ // so run ConstantVisitor for those to figure out if the initializer is
+ // actually a constant (and therefore side effect free to evaluate).
+ assert(!field.isConst);
+
+ var initializer = field.initializer;
+ if (initializer == null) return null;
+ return initializer.accept(_constantVisitor);
+ }
+}
« no previous file with comments | « lib/src/codegen/js_names.dart ('k') | lib/src/dependency_graph.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698