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

Side by Side Diff: pkg/analyzer/lib/src/generated/resolver.dart

Issue 1129563002: Create a class for evaluating const instance creation expressions. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 engine.resolver; 5 library engine.resolver;
6 6
7 import "dart:math" as math; 7 import "dart:math" as math;
8 import 'dart:collection'; 8 import 'dart:collection';
9 9
10 import 'package:analyzer/src/generated/utilities_collection.dart'; 10 import 'package:analyzer/src/generated/utilities_collection.dart';
(...skipping 806 matching lines...) Expand 10 before | Expand all | Expand 10 after
817 * The error reporter by which errors will be reported. 817 * The error reporter by which errors will be reported.
818 */ 818 */
819 final ErrorReporter _errorReporter; 819 final ErrorReporter _errorReporter;
820 820
821 /** 821 /**
822 * The type provider used to access the known types. 822 * The type provider used to access the known types.
823 */ 823 */
824 final TypeProvider _typeProvider; 824 final TypeProvider _typeProvider;
825 825
826 /** 826 /**
827 * The set of variables declared using '-D' on the command line.
828 */
829 final DeclaredVariables declaredVariables;
830
831 /**
827 * The type representing the type 'bool'. 832 * The type representing the type 'bool'.
828 */ 833 */
829 InterfaceType _boolType; 834 InterfaceType _boolType;
830 835
831 /** 836 /**
832 * The type representing the type 'int'. 837 * The type representing the type 'int'.
833 */ 838 */
834 InterfaceType _intType; 839 InterfaceType _intType;
835 840
836 /** 841 /**
837 * The type representing the type 'num'. 842 * The type representing the type 'num'.
838 */ 843 */
839 InterfaceType _numType; 844 InterfaceType _numType;
840 845
841 /** 846 /**
842 * The type representing the type 'string'. 847 * The type representing the type 'string'.
843 */ 848 */
844 InterfaceType _stringType; 849 InterfaceType _stringType;
845 850
846 /** 851 /**
847 * The current library that is being analyzed. 852 * The current library that is being analyzed.
848 */ 853 */
849 final LibraryElement _currentLibrary; 854 final LibraryElement _currentLibrary;
850 855
851 /** 856 /**
852 * Initialize a newly created constant verifier. 857 * Initialize a newly created constant verifier.
853 * 858 *
854 * @param errorReporter the error reporter by which errors will be reported 859 * @param errorReporter the error reporter by which errors will be reported
855 */ 860 */
856 ConstantVerifier( 861 ConstantVerifier(this._errorReporter, this._currentLibrary,
857 this._errorReporter, this._currentLibrary, this._typeProvider) { 862 this._typeProvider, this.declaredVariables) {
858 this._boolType = _typeProvider.boolType; 863 this._boolType = _typeProvider.boolType;
859 this._intType = _typeProvider.intType; 864 this._intType = _typeProvider.intType;
860 this._numType = _typeProvider.numType; 865 this._numType = _typeProvider.numType;
861 this._stringType = _typeProvider.stringType; 866 this._stringType = _typeProvider.stringType;
862 } 867 }
863 868
864 @override 869 @override
865 Object visitAnnotation(Annotation node) { 870 Object visitAnnotation(Annotation node) {
866 super.visitAnnotation(node); 871 super.visitAnnotation(node);
867 // check annotation creation 872 // check annotation creation
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
968 CompileTimeErrorCode.CONST_MAP_KEY_EXPRESSION_TYPE_IMPLEMENTS_EQ UALS, 973 CompileTimeErrorCode.CONST_MAP_KEY_EXPRESSION_TYPE_IMPLEMENTS_EQ UALS,
969 key, [type.displayName]); 974 key, [type.displayName]);
970 } 975 }
971 } 976 }
972 } else { 977 } else {
973 // Note: we throw the errors away because this isn't actually a const. 978 // Note: we throw the errors away because this isn't actually a const.
974 AnalysisErrorListener errorListener = 979 AnalysisErrorListener errorListener =
975 AnalysisErrorListener.NULL_LISTENER; 980 AnalysisErrorListener.NULL_LISTENER;
976 ErrorReporter subErrorReporter = 981 ErrorReporter subErrorReporter =
977 new ErrorReporter(errorListener, _errorReporter.source); 982 new ErrorReporter(errorListener, _errorReporter.source);
978 DartObjectImpl result = 983 DartObjectImpl result = key.accept(new ConstantVisitor(
979 key.accept(new ConstantVisitor(_typeProvider, subErrorReporter)); 984 new ConstantEvaluationEngine(_typeProvider, declaredVariables),
985 subErrorReporter));
980 if (result != null) { 986 if (result != null) {
981 if (keys.contains(result)) { 987 if (keys.contains(result)) {
982 invalidKeys.add(key); 988 invalidKeys.add(key);
983 } else { 989 } else {
984 keys.add(result); 990 keys.add(result);
985 } 991 }
986 } else { 992 } else {
987 reportEqualKeys = false; 993 reportEqualKeys = false;
988 } 994 }
989 } 995 }
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
1172 * time constant, or `null` if the expression is not a compile time constant. 1178 * time constant, or `null` if the expression is not a compile time constant.
1173 * 1179 *
1174 * @param expression the expression to be validated 1180 * @param expression the expression to be validated
1175 * @param errorCode the error code to be used if the expression is not a compi le time constant 1181 * @param errorCode the error code to be used if the expression is not a compi le time constant
1176 * @return the value of the compile time constant 1182 * @return the value of the compile time constant
1177 */ 1183 */
1178 DartObjectImpl _validate(Expression expression, ErrorCode errorCode) { 1184 DartObjectImpl _validate(Expression expression, ErrorCode errorCode) {
1179 RecordingErrorListener errorListener = new RecordingErrorListener(); 1185 RecordingErrorListener errorListener = new RecordingErrorListener();
1180 ErrorReporter subErrorReporter = 1186 ErrorReporter subErrorReporter =
1181 new ErrorReporter(errorListener, _errorReporter.source); 1187 new ErrorReporter(errorListener, _errorReporter.source);
1182 DartObjectImpl result = 1188 DartObjectImpl result = expression.accept(new ConstantVisitor(
1183 expression.accept(new ConstantVisitor(_typeProvider, subErrorReporter)); 1189 new ConstantEvaluationEngine(_typeProvider, declaredVariables),
1190 subErrorReporter));
1184 _reportErrors(errorListener.errors, errorCode); 1191 _reportErrors(errorListener.errors, errorCode);
1185 return result; 1192 return result;
1186 } 1193 }
1187 1194
1188 /** 1195 /**
1189 * Validate that if the passed arguments are constant expressions. 1196 * Validate that if the passed arguments are constant expressions.
1190 * 1197 *
1191 * @param argumentList the argument list to evaluate 1198 * @param argumentList the argument list to evaluate
1192 */ 1199 */
1193 void _validateConstantArguments(ArgumentList argumentList) { 1200 void _validateConstantArguments(ArgumentList argumentList) {
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
1279 for (VariableDeclaration variableDeclaration 1286 for (VariableDeclaration variableDeclaration
1280 in fieldDeclaration.fields.variables) { 1287 in fieldDeclaration.fields.variables) {
1281 Expression initializer = variableDeclaration.initializer; 1288 Expression initializer = variableDeclaration.initializer;
1282 if (initializer != null) { 1289 if (initializer != null) {
1283 // Ignore any errors produced during validation--if the constant 1290 // Ignore any errors produced during validation--if the constant
1284 // can't be eavluated we'll just report a single error. 1291 // can't be eavluated we'll just report a single error.
1285 AnalysisErrorListener errorListener = 1292 AnalysisErrorListener errorListener =
1286 AnalysisErrorListener.NULL_LISTENER; 1293 AnalysisErrorListener.NULL_LISTENER;
1287 ErrorReporter subErrorReporter = 1294 ErrorReporter subErrorReporter =
1288 new ErrorReporter(errorListener, _errorReporter.source); 1295 new ErrorReporter(errorListener, _errorReporter.source);
1289 DartObjectImpl result = initializer 1296 DartObjectImpl result = initializer.accept(new ConstantVisitor(
1290 .accept(new ConstantVisitor(_typeProvider, subErrorReporter)); 1297 new ConstantEvaluationEngine(
1298 _typeProvider, declaredVariables), subErrorReporter));
1291 if (result == null) { 1299 if (result == null) {
1292 _errorReporter.reportErrorForNode( 1300 _errorReporter.reportErrorForNode(
1293 CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_FIELD_INITIALIZE D_BY_NON_CONST, 1301 CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_FIELD_INITIALIZE D_BY_NON_CONST,
1294 errorSite, [variableDeclaration.name.name]); 1302 errorSite, [variableDeclaration.name.name]);
1295 } 1303 }
1296 } 1304 }
1297 } 1305 }
1298 } 1306 }
1299 } 1307 }
1300 } 1308 }
1301 } 1309 }
1302 1310
1303 /** 1311 /**
1304 * Validates that the given expression is a compile time constant. 1312 * Validates that the given expression is a compile time constant.
1305 * 1313 *
1306 * @param parameterElements the elements of parameters of constant constructor , they are 1314 * @param parameterElements the elements of parameters of constant constructor , they are
1307 * considered as a valid potentially constant expressions 1315 * considered as a valid potentially constant expressions
1308 * @param expression the expression to validate 1316 * @param expression the expression to validate
1309 */ 1317 */
1310 void _validateInitializerExpression( 1318 void _validateInitializerExpression(
1311 List<ParameterElement> parameterElements, Expression expression) { 1319 List<ParameterElement> parameterElements, Expression expression) {
1312 RecordingErrorListener errorListener = new RecordingErrorListener(); 1320 RecordingErrorListener errorListener = new RecordingErrorListener();
1313 ErrorReporter subErrorReporter = 1321 ErrorReporter subErrorReporter =
1314 new ErrorReporter(errorListener, _errorReporter.source); 1322 new ErrorReporter(errorListener, _errorReporter.source);
1315 DartObjectImpl result = expression.accept( 1323 DartObjectImpl result = expression.accept(
1316 new _ConstantVerifier_validateInitializerExpression( 1324 new _ConstantVerifier_validateInitializerExpression(_typeProvider,
1317 _typeProvider, subErrorReporter, this, parameterElements)); 1325 subErrorReporter, this, parameterElements, declaredVariables));
1318 _reportErrors(errorListener.errors, 1326 _reportErrors(errorListener.errors,
1319 CompileTimeErrorCode.NON_CONSTANT_VALUE_IN_INITIALIZER); 1327 CompileTimeErrorCode.NON_CONSTANT_VALUE_IN_INITIALIZER);
1320 if (result != null) { 1328 if (result != null) {
1321 _reportErrorIfFromDeferredLibrary(expression, 1329 _reportErrorIfFromDeferredLibrary(expression,
1322 CompileTimeErrorCode.NON_CONSTANT_VALUE_IN_INITIALIZER_FROM_DEFERRED_L IBRARY); 1330 CompileTimeErrorCode.NON_CONSTANT_VALUE_IN_INITIALIZER_FROM_DEFERRED_L IBRARY);
1323 } 1331 }
1324 } 1332 }
1325 1333
1326 /** 1334 /**
1327 * Validates that all of the arguments of a constructor initializer are compil e time constants. 1335 * Validates that all of the arguments of a constructor initializer are compil e time constants.
(...skipping 7136 matching lines...) Expand 10 before | Expand all | Expand 10 after
8464 computer.computeValues(); 8472 computer.computeValues();
8465 // As a temporary workaround for issue 21572, run ConstantVerifier now. 8473 // As a temporary workaround for issue 21572, run ConstantVerifier now.
8466 // TODO(paulberry): remove this workaround once issue 21572 is fixed. 8474 // TODO(paulberry): remove this workaround once issue 21572 is fixed.
8467 for (Library library in _librariesInCycles) { 8475 for (Library library in _librariesInCycles) {
8468 for (Source source in library.compilationUnitSources) { 8476 for (Source source in library.compilationUnitSources) {
8469 try { 8477 try {
8470 CompilationUnit unit = library.getAST(source); 8478 CompilationUnit unit = library.getAST(source);
8471 ErrorReporter errorReporter = 8479 ErrorReporter errorReporter =
8472 new ErrorReporter(_errorListener, source); 8480 new ErrorReporter(_errorListener, source);
8473 ConstantVerifier constantVerifier = new ConstantVerifier( 8481 ConstantVerifier constantVerifier = new ConstantVerifier(
8474 errorReporter, library.libraryElement, _typeProvider); 8482 errorReporter, library.libraryElement, _typeProvider,
8483 analysisContext.declaredVariables);
8475 unit.accept(constantVerifier); 8484 unit.accept(constantVerifier);
8476 } on AnalysisException catch (exception, stackTrace) { 8485 } on AnalysisException catch (exception, stackTrace) {
8477 AnalysisEngine.instance.logger.logError( 8486 AnalysisEngine.instance.logger.logError(
8478 "Internal Error: Could not access AST for ${source.fullName} " 8487 "Internal Error: Could not access AST for ${source.fullName} "
8479 "during constant verification", 8488 "during constant verification",
8480 new CaughtException(exception, stackTrace)); 8489 new CaughtException(exception, stackTrace));
8481 } 8490 }
8482 } 8491 }
8483 } 8492 }
8484 }); 8493 });
(...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after
8988 computer.computeValues(); 8997 computer.computeValues();
8989 // As a temporary workaround for issue 21572, run ConstantVerifier now. 8998 // As a temporary workaround for issue 21572, run ConstantVerifier now.
8990 // TODO(paulberry): remove this workaround once issue 21572 is fixed. 8999 // TODO(paulberry): remove this workaround once issue 21572 is fixed.
8991 for (ResolvableLibrary library in _librariesInCycle) { 9000 for (ResolvableLibrary library in _librariesInCycle) {
8992 for (ResolvableCompilationUnit unit 9001 for (ResolvableCompilationUnit unit
8993 in library.resolvableCompilationUnits) { 9002 in library.resolvableCompilationUnits) {
8994 CompilationUnit ast = unit.compilationUnit; 9003 CompilationUnit ast = unit.compilationUnit;
8995 ErrorReporter errorReporter = 9004 ErrorReporter errorReporter =
8996 new ErrorReporter(_errorListener, unit.source); 9005 new ErrorReporter(_errorListener, unit.source);
8997 ConstantVerifier constantVerifier = new ConstantVerifier( 9006 ConstantVerifier constantVerifier = new ConstantVerifier(
8998 errorReporter, library.libraryElement, _typeProvider); 9007 errorReporter, library.libraryElement, _typeProvider,
9008 analysisContext.declaredVariables);
8999 ast.accept(constantVerifier); 9009 ast.accept(constantVerifier);
9000 } 9010 }
9001 } 9011 }
9002 }); 9012 });
9003 } 9013 }
9004 9014
9005 /** 9015 /**
9006 * Resolve the identifiers and perform type analysis in the libraries in the c urrent cycle. 9016 * Resolve the identifiers and perform type analysis in the libraries in the c urrent cycle.
9007 * 9017 *
9008 * @throws AnalysisException if any of the identifiers could not be resolved o r if any of the 9018 * @throws AnalysisException if any of the identifiers could not be resolved o r if any of the
(...skipping 6258 matching lines...) Expand 10 before | Expand all | Expand 10 after
15267 } 15277 }
15268 return null; 15278 return null;
15269 } 15279 }
15270 } 15280 }
15271 15281
15272 class _ConstantVerifier_validateInitializerExpression extends ConstantVisitor { 15282 class _ConstantVerifier_validateInitializerExpression extends ConstantVisitor {
15273 final ConstantVerifier verifier; 15283 final ConstantVerifier verifier;
15274 15284
15275 List<ParameterElement> parameterElements; 15285 List<ParameterElement> parameterElements;
15276 15286
15277 _ConstantVerifier_validateInitializerExpression(TypeProvider arg0, 15287 _ConstantVerifier_validateInitializerExpression(TypeProvider typeProvider,
15278 ErrorReporter arg1, this.verifier, this.parameterElements) 15288 ErrorReporter errorReporter, this.verifier, this.parameterElements,
15279 : super(arg0, arg1); 15289 DeclaredVariables declaredVariables)
15290 : super(new ConstantEvaluationEngine(typeProvider, declaredVariables),
15291 errorReporter);
15280 15292
15281 @override 15293 @override
15282 DartObjectImpl visitSimpleIdentifier(SimpleIdentifier node) { 15294 DartObjectImpl visitSimpleIdentifier(SimpleIdentifier node) {
15283 Element element = node.staticElement; 15295 Element element = node.staticElement;
15284 for (ParameterElement parameterElement in parameterElements) { 15296 for (ParameterElement parameterElement in parameterElements) {
15285 if (identical(parameterElement, element) && parameterElement != null) { 15297 if (identical(parameterElement, element) && parameterElement != null) {
15286 DartType type = parameterElement.type; 15298 DartType type = parameterElement.type;
15287 if (type != null) { 15299 if (type != null) {
15288 if (type.isDynamic) { 15300 if (type.isDynamic) {
15289 return new DartObjectImpl( 15301 return new DartObjectImpl(
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
15426 nonFields.add(node); 15438 nonFields.add(node);
15427 return null; 15439 return null;
15428 } 15440 }
15429 15441
15430 @override 15442 @override
15431 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this); 15443 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this);
15432 15444
15433 @override 15445 @override
15434 Object visitWithClause(WithClause node) => null; 15446 Object visitWithClause(WithClause node) => null;
15435 } 15447 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/generated/incremental_resolver.dart ('k') | pkg/analyzer/test/generated/all_the_rest_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698