OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library dev_compiler.src.codegen.side_effect_analysis; | 5 library dev_compiler.src.codegen.side_effect_analysis; |
6 | 6 |
7 import 'package:analyzer/src/generated/ast.dart'; | 7 import 'package:analyzer/src/generated/ast.dart'; |
| 8 import 'package:analyzer/src/generated/constant.dart'; |
8 import 'package:analyzer/src/generated/element.dart'; | 9 import 'package:analyzer/src/generated/element.dart'; |
| 10 import 'package:analyzer/src/generated/error.dart' show ErrorReporter; |
| 11 import 'package:analyzer/src/generated/engine.dart' show RecordingErrorListener; |
| 12 import 'package:analyzer/src/generated/resolver.dart' show TypeProvider; |
9 | 13 |
10 /// True is the expression can be evaluated multiple times without causing | 14 /// True is the expression can be evaluated multiple times without causing |
11 /// code execution. This is true for final fields. This can be true for local | 15 /// code execution. This is true for final fields. This can be true for local |
12 /// variables, if: | 16 /// variables, if: |
13 /// * they are not assigned within the [context]. | 17 /// * they are not assigned within the [context]. |
14 /// * they are not assigned in a function closure anywhere. | 18 /// * they are not assigned in a function closure anywhere. |
15 /// True is the expression can be evaluated multiple times without causing | 19 /// True is the expression can be evaluated multiple times without causing |
16 /// code execution. This is true for final fields. This can be true for local | 20 /// code execution. This is true for final fields. This can be true for local |
17 /// variables, if: | 21 /// variables, if: |
18 /// | 22 /// |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 if (parent is MethodInvocation && | 83 if (parent is MethodInvocation && |
80 identical(parent.methodName, node)) return; | 84 identical(parent.methodName, node)) return; |
81 if (parent is ConstructorName) return; | 85 if (parent is ConstructorName) return; |
82 if (parent is Label) return; | 86 if (parent is Label) return; |
83 | 87 |
84 if (node.inSetterContext() && node.staticElement == _variable) { | 88 if (node.inSetterContext() && node.staticElement == _variable) { |
85 _potentiallyMutated = true; | 89 _potentiallyMutated = true; |
86 } | 90 } |
87 } | 91 } |
88 } | 92 } |
| 93 |
| 94 class ConstFieldVisitor { |
| 95 final ConstantVisitor _constantVisitor; |
| 96 |
| 97 ConstFieldVisitor(TypeProvider types, CompilationUnit unit) |
| 98 : _constantVisitor = new ConstantVisitor.con1(types, |
| 99 new ErrorReporter(new RecordingErrorListener(), unit.element.source)); |
| 100 |
| 101 // TODO(jmesserly): this is used to determine if the field initialization is |
| 102 // side effect free. We should make the check more general, as things like |
| 103 // list/map literals/regexp are also side effect free and fairly common |
| 104 // to use as field initializers. |
| 105 bool isFieldInitConstant(VariableDeclaration field) => |
| 106 field.initializer == null || computeConstant(field) != null; |
| 107 |
| 108 DartObjectImpl computeConstant(VariableDeclaration field) { |
| 109 // If the constant is already computed by ConstantEvaluator, just return it. |
| 110 VariableElementImpl element = field.element; |
| 111 var result = element.evaluationResult; |
| 112 if (result != null) return result.value; |
| 113 |
| 114 // ConstantEvaluator will not compute constants for non-const fields, |
| 115 // so run ConstantVisitor for those to figure out if the initializer is |
| 116 // actually a constant (and therefore side effect free to evaluate). |
| 117 assert(!field.isConst); |
| 118 |
| 119 var initializer = field.initializer; |
| 120 if (initializer == null) return null; |
| 121 return initializer.accept(_constantVisitor); |
| 122 } |
| 123 } |
OLD | NEW |