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

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

Issue 633803002: Check parameter types when evaluating constants. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 2 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
« no previous file with comments | « pkg/analyzer/lib/src/error_formatter.dart ('k') | pkg/analyzer/lib/src/generated/error.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // This code was auto-generated, is not intended to be edited, and is subject to 5 // This code was auto-generated, is not intended to be edited, and is subject to
6 // significant change. Please see the README file for more information. 6 // significant change. Please see the README file for more information.
7 7
8 library engine.constant; 8 library engine.constant;
9 9
10 import 'dart:collection'; 10 import 'dart:collection';
(...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 if (!identical(argumentValues[0].type, typeProvider.stringType)) { 499 if (!identical(argumentValues[0].type, typeProvider.stringType)) {
500 return false; 500 return false;
501 } 501 }
502 if (argumentCount == 2) { 502 if (argumentCount == 2) {
503 if (arguments[1] is! NamedExpression) { 503 if (arguments[1] is! NamedExpression) {
504 return false; 504 return false;
505 } 505 }
506 if (!((arguments[1] as NamedExpression).name.label.name == _DEFAULT_VALUE_ PARAM)) { 506 if (!((arguments[1] as NamedExpression).name.label.name == _DEFAULT_VALUE_ PARAM)) {
507 return false; 507 return false;
508 } 508 }
509 InterfaceType defaultValueType = namedArgumentValues[_DEFAULT_VALUE_PARAM] .type; 509 ParameterizedType defaultValueType = namedArgumentValues[_DEFAULT_VALUE_PA RAM].type;
510 if (!(identical(defaultValueType, expectedDefaultValueType) || identical(d efaultValueType, typeProvider.nullType))) { 510 if (!(identical(defaultValueType, expectedDefaultValueType) || identical(d efaultValueType, typeProvider.nullType))) {
511 return false; 511 return false;
512 } 512 }
513 } 513 }
514 return true; 514 return true;
515 } 515 }
516 516
517 /** 517 /**
518 * Check that the arguments to a call to Symbol() are correct. 518 * Check that the arguments to a call to Symbol() are correct.
519 * 519 *
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
616 // because it's likely to lead to cascading errors. So just leave [valu e] in the unknown 616 // because it's likely to lead to cascading errors. So just leave [valu e] in the unknown
617 // state. 617 // state.
618 } 618 }
619 } 619 }
620 return value; 620 return value;
621 } 621 }
622 622
623 DartObjectImpl _evaluateConstructorCall(AstNode node, NodeList<Expression> arg uments, ConstructorElement constructor, ConstantVisitor constantVisitor, ErrorRe porter errorReporter) { 623 DartObjectImpl _evaluateConstructorCall(AstNode node, NodeList<Expression> arg uments, ConstructorElement constructor, ConstantVisitor constantVisitor, ErrorRe porter errorReporter) {
624 int argumentCount = arguments.length; 624 int argumentCount = arguments.length;
625 List<DartObjectImpl> argumentValues = new List<DartObjectImpl>(argumentCount ); 625 List<DartObjectImpl> argumentValues = new List<DartObjectImpl>(argumentCount );
626 List<Expression> argumentNodes = new List<Expression>(argumentCount);
626 HashMap<String, DartObjectImpl> namedArgumentValues = new HashMap<String, Da rtObjectImpl>(); 627 HashMap<String, DartObjectImpl> namedArgumentValues = new HashMap<String, Da rtObjectImpl>();
628 HashMap<String, NamedExpression> namedArgumentNodes =
629 new HashMap<String, NamedExpression>();
627 for (int i = 0; i < argumentCount; i++) { 630 for (int i = 0; i < argumentCount; i++) {
628 Expression argument = arguments[i]; 631 Expression argument = arguments[i];
629 if (argument is NamedExpression) { 632 if (argument is NamedExpression) {
630 NamedExpression namedExpression = argument; 633 String name = argument.name.label.name;
631 String name = namedExpression.name.label.name; 634 namedArgumentValues[name] = constantVisitor._valueOf(argument.expression );
632 namedArgumentValues[name] = constantVisitor._valueOf(namedExpression.exp ression); 635 namedArgumentNodes[name] = argument;
633 argumentValues[i] = constantVisitor.null2; 636 argumentValues[i] = constantVisitor.null2;
634 } else { 637 } else {
635 argumentValues[i] = constantVisitor._valueOf(argument); 638 argumentValues[i] = constantVisitor._valueOf(argument);
639 argumentNodes[i] = argument;
636 } 640 }
637 } 641 }
638 constructor = _followConstantRedirectionChain(constructor); 642 constructor = _followConstantRedirectionChain(constructor);
639 InterfaceType definingClass = constructor.returnType as InterfaceType; 643 InterfaceType definingClass = constructor.returnType as InterfaceType;
640 if (constructor.isFactory) { 644 if (constructor.isFactory) {
641 // We couldn't find a non-factory constructor. See if it's because we rea ched an external 645 // We couldn't find a non-factory constructor. See if it's because we rea ched an external
642 // const factory constructor that we can emulate. 646 // const factory constructor that we can emulate.
643 if (constructor.name == "fromEnvironment") { 647 if (constructor.name == "fromEnvironment") {
644 if (!_checkFromEnvironmentArguments(arguments, argumentValues, namedArgu mentValues, definingClass)) { 648 if (!_checkFromEnvironmentArguments(arguments, argumentValues, namedArgu mentValues, definingClass)) {
645 errorReporter.reportErrorForNode(CompileTimeErrorCode.CONST_EVAL_THROW S_EXCEPTION, node, []); 649 errorReporter.reportErrorForNode(CompileTimeErrorCode.CONST_EVAL_THROW S_EXCEPTION, node, []);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
679 List<ConstructorInitializer> initializers = constructorBase.constantInitiali zers; 683 List<ConstructorInitializer> initializers = constructorBase.constantInitiali zers;
680 if (initializers == null) { 684 if (initializers == null) {
681 // This can happen in some cases where there are compile errors in the cod e being analyzed 685 // This can happen in some cases where there are compile errors in the cod e being analyzed
682 // (for example if the code is trying to create a const instance using a n on-const 686 // (for example if the code is trying to create a const instance using a n on-const
683 // constructor, or the node we're visiting is involved in a cycle). The e rror has already 687 // constructor, or the node we're visiting is involved in a cycle). The e rror has already
684 // been reported, so consider it an unknown value to suppress further erro rs. 688 // been reported, so consider it an unknown value to suppress further erro rs.
685 return constantVisitor._validWithUnknownValue(definingClass); 689 return constantVisitor._validWithUnknownValue(definingClass);
686 } 690 }
687 HashMap<String, DartObjectImpl> fieldMap = new HashMap<String, DartObjectImp l>(); 691 HashMap<String, DartObjectImpl> fieldMap = new HashMap<String, DartObjectImp l>();
688 HashMap<String, DartObjectImpl> parameterMap = new HashMap<String, DartObjec tImpl>(); 692 HashMap<String, DartObjectImpl> parameterMap = new HashMap<String, DartObjec tImpl>();
689 List<ParameterElement> parameters = constructorBase.parameters; 693 List<ParameterElement> parameters = constructor.parameters;
690 int parameterCount = parameters.length; 694 int parameterCount = parameters.length;
691 for (int i = 0; i < parameterCount; i++) { 695 for (int i = 0; i < parameterCount; i++) {
692 ParameterElement parameter = parameters[i]; 696 ParameterElement parameter = parameters[i];
693 while (parameter is ParameterMember) { 697 ParameterElement baseParameter = parameter;
694 parameter = (parameter as ParameterMember).baseElement; 698 while (baseParameter is ParameterMember) {
699 baseParameter = (baseParameter as ParameterMember).baseElement;
695 } 700 }
696 DartObjectImpl argumentValue = null; 701 DartObjectImpl argumentValue = null;
697 if (parameter.parameterKind == ParameterKind.NAMED) { 702 AstNode errorTarget = null;
698 argumentValue = namedArgumentValues[parameter.name]; 703 if (baseParameter.parameterKind == ParameterKind.NAMED) {
704 argumentValue = namedArgumentValues[baseParameter.name];
705 errorTarget = namedArgumentNodes[baseParameter.name];
699 } else if (i < argumentCount) { 706 } else if (i < argumentCount) {
700 argumentValue = argumentValues[i]; 707 argumentValue = argumentValues[i];
708 errorTarget = argumentNodes[i];
701 } 709 }
702 if (argumentValue == null && parameter is ParameterElementImpl) { 710 if (errorTarget == null) {
711 // No argument node that we can direct error messages to, because we
712 // are handling an optional parameter that wasn't specified. So just
713 // direct error messages to the constructor call.
714 errorTarget = node;
715 }
716 if (argumentValue == null && baseParameter is ParameterElementImpl) {
703 // The parameter is an optional positional parameter for which no value was provided, so 717 // The parameter is an optional positional parameter for which no value was provided, so
704 // use the default value. 718 // use the default value.
705 beforeGetParameterDefault(parameter); 719 beforeGetParameterDefault(baseParameter);
706 EvaluationResultImpl evaluationResult = (parameter as ParameterElementIm pl).evaluationResult; 720 EvaluationResultImpl evaluationResult = (baseParameter as ParameterEleme ntImpl).evaluationResult;
707 if (evaluationResult == null) { 721 if (evaluationResult == null) {
708 // No default was provided, so the default value is null. 722 // No default was provided, so the default value is null.
709 argumentValue = constantVisitor.null2; 723 argumentValue = constantVisitor.null2;
710 } else if (evaluationResult.value != null) { 724 } else if (evaluationResult.value != null) {
711 argumentValue = evaluationResult.value; 725 argumentValue = evaluationResult.value;
712 } 726 }
713 } 727 }
714 if (argumentValue != null) { 728 if (argumentValue != null) {
715 if (parameter.isInitializingFormal) { 729 if (!argumentValue.isNull && !argumentValue.type.isSubtypeOf(parameter.t ype)) {
Brian Wilkerson 2014/10/07 16:30:48 We should perform this test before we access the d
Paul Berry 2014/10/07 17:03:34 The difficulty with that is that it doesn't have c
716 FieldElement field = (parameter as FieldFormalParameterElement).field; 730 errorReporter.reportErrorForNode(
731 CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMA TCH,
732 errorTarget,
733 [argumentValue.type, parameter.type]);
734 }
735 if (baseParameter.isInitializingFormal) {
736 FieldElement field = (baseParameter as FieldFormalParameterElement).fi eld;
717 if (field != null) { 737 if (field != null) {
718 String fieldName = field.name; 738 String fieldName = field.name;
719 fieldMap[fieldName] = argumentValue; 739 fieldMap[fieldName] = argumentValue;
720 } 740 }
721 } else { 741 } else {
722 String name = parameter.name; 742 String name = baseParameter.name;
723 parameterMap[name] = argumentValue; 743 parameterMap[name] = argumentValue;
724 } 744 }
725 } 745 }
726 } 746 }
727 ConstantVisitor initializerVisitor = new ConstantVisitor.con2(typeProvider, parameterMap, errorReporter); 747 ConstantVisitor initializerVisitor = new ConstantVisitor.con2(typeProvider, parameterMap, errorReporter);
728 String superName = null; 748 String superName = null;
729 NodeList<Expression> superArguments = null; 749 NodeList<Expression> superArguments = null;
730 for (ConstructorInitializer initializer in initializers) { 750 for (ConstructorInitializer initializer in initializers) {
731 if (initializer is ConstructorFieldInitializer) { 751 if (initializer is ConstructorFieldInitializer) {
732 ConstructorFieldInitializer constructorFieldInitializer = initializer; 752 ConstructorFieldInitializer constructorFieldInitializer = initializer;
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
1040 } 1060 }
1041 conditionResult = _dartObjectComputer.applyBooleanConversion(condition, cond itionResult); 1061 conditionResult = _dartObjectComputer.applyBooleanConversion(condition, cond itionResult);
1042 if (conditionResult == null) { 1062 if (conditionResult == null) {
1043 return conditionResult; 1063 return conditionResult;
1044 } 1064 }
1045 if (conditionResult.isTrue) { 1065 if (conditionResult.isTrue) {
1046 return thenResult; 1066 return thenResult;
1047 } else if (conditionResult.isFalse) { 1067 } else if (conditionResult.isFalse) {
1048 return elseResult; 1068 return elseResult;
1049 } 1069 }
1050 InterfaceType thenType = thenResult.type; 1070 ParameterizedType thenType = thenResult.type;
1051 InterfaceType elseType = elseResult.type; 1071 ParameterizedType elseType = elseResult.type;
1052 return _validWithUnknownValue(thenType.getLeastUpperBound(elseType) as Inter faceType); 1072 return _validWithUnknownValue(thenType.getLeastUpperBound(elseType) as Inter faceType);
1053 } 1073 }
1054 1074
1055 @override 1075 @override
1056 DartObjectImpl visitDoubleLiteral(DoubleLiteral node) => new DartObjectImpl(_t ypeProvider.doubleType, new DoubleState(node.value)); 1076 DartObjectImpl visitDoubleLiteral(DoubleLiteral node) => new DartObjectImpl(_t ypeProvider.doubleType, new DoubleState(node.value));
1057 1077
1058 @override 1078 @override
1059 DartObjectImpl visitInstanceCreationExpression(InstanceCreationExpression node ) { 1079 DartObjectImpl visitInstanceCreationExpression(InstanceCreationExpression node ) {
1060 if (!node.isConst) { 1080 if (!node.isConst) {
1061 // TODO(brianwilkerson) Figure out which error to report. 1081 // TODO(brianwilkerson) Figure out which error to report.
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
1342 if (element is VariableElementImpl) { 1362 if (element is VariableElementImpl) {
1343 VariableElementImpl variableElementImpl = element; 1363 VariableElementImpl variableElementImpl = element;
1344 beforeGetEvaluationResult(node); 1364 beforeGetEvaluationResult(node);
1345 EvaluationResultImpl value = variableElementImpl.evaluationResult; 1365 EvaluationResultImpl value = variableElementImpl.evaluationResult;
1346 if (variableElementImpl.isConst && value != null) { 1366 if (variableElementImpl.isConst && value != null) {
1347 return value.value; 1367 return value.value;
1348 } 1368 }
1349 } else if (element is ExecutableElement) { 1369 } else if (element is ExecutableElement) {
1350 ExecutableElement function = element; 1370 ExecutableElement function = element;
1351 if (function.isStatic) { 1371 if (function.isStatic) {
1352 return new DartObjectImpl(_typeProvider.functionType, new FunctionState( function)); 1372 ParameterizedType functionType = function.type;
1373 if (functionType == null) {
1374 functionType = _typeProvider.functionType;
1375 }
1376 return new DartObjectImpl(functionType, new FunctionState(function));
1353 } 1377 }
1354 } else if (element is ClassElement || element is FunctionTypeAliasElement) { 1378 } else if (element is ClassElement || element is FunctionTypeAliasElement) {
1355 return new DartObjectImpl(_typeProvider.typeType, new TypeState(element)); 1379 return new DartObjectImpl(_typeProvider.typeType, new TypeState(element));
1356 } 1380 }
1357 // TODO(brianwilkerson) Figure out which error to report. 1381 // TODO(brianwilkerson) Figure out which error to report.
1358 _error(node, null); 1382 _error(node, null);
1359 return null; 1383 return null;
1360 } 1384 }
1361 1385
1362 /** 1386 /**
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
1413 * 1437 *
1414 * @return the string value of this object 1438 * @return the string value of this object
1415 */ 1439 */
1416 String get stringValue; 1440 String get stringValue;
1417 1441
1418 /** 1442 /**
1419 * Return the run-time type of this object. 1443 * Return the run-time type of this object.
1420 * 1444 *
1421 * @return the run-time type of this object 1445 * @return the run-time type of this object
1422 */ 1446 */
1423 InterfaceType get type; 1447 ParameterizedType get type;
1424 1448
1425 /** 1449 /**
1426 * Return this object's value if it can be represented exactly, or `null` if e ither the 1450 * Return this object's value if it can be represented exactly, or `null` if e ither the
1427 * value cannot be represented exactly or if the value is `null`. Clients shou ld use 1451 * value cannot be represented exactly or if the value is `null`. Clients shou ld use
1428 * [hasExactValue] to distinguish between these two cases. 1452 * [hasExactValue] to distinguish between these two cases.
1429 * 1453 *
1430 * @return this object's value 1454 * @return this object's value
1431 */ 1455 */
1432 Object get value; 1456 Object get value;
1433 1457
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after
1778 } 1802 }
1779 } 1803 }
1780 1804
1781 /** 1805 /**
1782 * Instances of the class `DartObjectImpl` represent an instance of a Dart class . 1806 * Instances of the class `DartObjectImpl` represent an instance of a Dart class .
1783 */ 1807 */
1784 class DartObjectImpl implements DartObject { 1808 class DartObjectImpl implements DartObject {
1785 /** 1809 /**
1786 * The run-time type of this object. 1810 * The run-time type of this object.
1787 */ 1811 */
1788 final InterfaceType type; 1812 final ParameterizedType type;
1789 1813
1790 /** 1814 /**
1791 * The state of the object. 1815 * The state of the object.
1792 */ 1816 */
1793 final InstanceState _state; 1817 final InstanceState _state;
1794 1818
1795 /** 1819 /**
1796 * Initialize a newly created object to have the given type and state. 1820 * Initialize a newly created object to have the given type and state.
1797 * 1821 *
1798 * @param type the run-time type of this object 1822 * @param type the run-time type of this object
(...skipping 2769 matching lines...) Expand 10 before | Expand all | Expand 10 after
4568 4592
4569 @override 4593 @override
4570 String get typeName => "Type"; 4594 String get typeName => "Type";
4571 4595
4572 @override 4596 @override
4573 int get hashCode => _element == null ? 0 : _element.hashCode; 4597 int get hashCode => _element == null ? 0 : _element.hashCode;
4574 4598
4575 @override 4599 @override
4576 String toString() => _element == null ? "-unknown-" : _element.name; 4600 String toString() => _element == null ? "-unknown-" : _element.name;
4577 } 4601 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/error_formatter.dart ('k') | pkg/analyzer/lib/src/generated/error.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698