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

Side by Side Diff: pkg/dev_compiler/lib/src/compiler/side_effect_analysis.dart

Issue 2757753002: Migrate DDC to the new analysis driver.
Patch Set: Rebase Created 3 years, 6 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 unified diff | Download patch
OLDNEW
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 import 'package:analyzer/dart/ast/ast.dart'; 5 import 'package:analyzer/dart/ast/ast.dart';
6 import 'package:analyzer/dart/ast/visitor.dart'; 6 import 'package:analyzer/dart/ast/visitor.dart';
7 import 'package:analyzer/dart/element/element.dart'; 7 import 'package:analyzer/dart/element/element.dart';
8 import 'package:analyzer/src/generated/constant.dart'; 8 import 'package:analyzer/src/generated/constant.dart';
9 import 'package:analyzer/error/listener.dart' 9 import 'package:analyzer/error/listener.dart'
10 show AnalysisErrorListener, ErrorReporter; 10 show AnalysisErrorListener, ErrorReporter;
11 import 'package:analyzer/src/generated/engine.dart'; 11 import 'package:analyzer/src/generated/resolver.dart';
12 import 'package:analyzer/src/generated/source.dart' show Source; 12 import 'package:analyzer/src/generated/source.dart' show Source;
13 import 'package:analyzer/src/dart/ast/ast.dart'; 13 import 'package:analyzer/src/dart/ast/ast.dart';
14 14
15 /// True is the expression can be evaluated multiple times without causing 15 /// 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 16 /// code execution. This is true for final fields. This can be true for local
17 /// variables, if: 17 /// variables, if:
18 /// * they are not assigned within the [context]. 18 /// * they are not assigned within the [context].
19 /// * they are not assigned in a function closure anywhere. 19 /// * they are not assigned in a function closure anywhere.
20 /// True is the expression can be evaluated multiple times without causing 20 /// True is the expression can be evaluated multiple times without causing
21 /// code execution. This is true for final fields. This can be true for local 21 /// code execution. This is true for final fields. This can be true for local
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 100
101 if (node.inSetterContext() && node.staticElement == _variable) { 101 if (node.inSetterContext() && node.staticElement == _variable) {
102 _potentiallyMutated = true; 102 _potentiallyMutated = true;
103 } 103 }
104 } 104 }
105 } 105 }
106 106
107 class ConstFieldVisitor { 107 class ConstFieldVisitor {
108 final ConstantVisitor constantVisitor; 108 final ConstantVisitor constantVisitor;
109 109
110 ConstFieldVisitor(AnalysisContext context, {Source dummySource}) 110 ConstFieldVisitor(
111 TypeProvider typeProvider, DeclaredVariables declaredVariables,
112 {Source dummySource})
111 : constantVisitor = new ConstantVisitor( 113 : constantVisitor = new ConstantVisitor(
112 new ConstantEvaluationEngine( 114 new ConstantEvaluationEngine(typeProvider, declaredVariables),
113 context.typeProvider, context.declaredVariables),
114 new ErrorReporter( 115 new ErrorReporter(
115 AnalysisErrorListener.NULL_LISTENER, dummySource)); 116 AnalysisErrorListener.NULL_LISTENER, dummySource));
116 117
117 // TODO(jmesserly): this is used to determine if the field initialization is 118 // TODO(jmesserly): this is used to determine if the field initialization is
118 // side effect free. We should make the check more general, as things like 119 // side effect free. We should make the check more general, as things like
119 // list/map literals/regexp are also side effect free and fairly common 120 // list/map literals/regexp are also side effect free and fairly common
120 // to use as field initializers. 121 // to use as field initializers.
121 bool isFieldInitConstant(VariableDeclaration field) => 122 bool isFieldInitConstant(VariableDeclaration field) =>
122 field.initializer == null || computeConstant(field) != null; 123 field.initializer == null || computeConstant(field) != null;
123 124
124 DartObject computeConstant(VariableDeclaration field) { 125 DartObject computeConstant(VariableDeclaration field) {
125 // If the constant is already computed by ConstantEvaluator, just return it. 126 // If the constant is already computed by ConstantEvaluator, just return it.
126 VariableElement element = field.element; 127 VariableElement element = field.element;
127 var result = element.computeConstantValue(); 128 var result = element.constantValue;
128 if (result != null) return result; 129 if (result != null) return result;
129 130
130 // ConstantEvaluator will not compute constants for non-const fields, 131 // ConstantEvaluator will not compute constants for non-const fields,
131 // so run ConstantVisitor for those to figure out if the initializer is 132 // so run ConstantVisitor for those to figure out if the initializer is
132 // actually a constant (and therefore side effect free to evaluate). 133 // actually a constant (and therefore side effect free to evaluate).
133 assert(!field.isConst); 134 assert(!field.isConst);
134 135
135 var initializer = field.initializer; 136 var initializer = field.initializer;
136 if (initializer == null) return null; 137 if (initializer == null) return null;
137 return initializer.accept(constantVisitor); 138 return initializer.accept(constantVisitor);
138 } 139 }
139 } 140 }
OLDNEW
« no previous file with comments | « pkg/dev_compiler/lib/src/compiler/nullable_type_inference.dart ('k') | pkg/dev_compiler/test/codegen_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698