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

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

Issue 1050203002: Begin making copies of AST nodes for constants during resolution. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address review comments and switch to using mixins. Created 5 years, 8 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/generated/ast.dart ('k') | pkg/analyzer/lib/src/generated/element.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 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 138
139 /** 139 /**
140 * Return the boolean state representing the given boolean [value]. 140 * Return the boolean state representing the given boolean [value].
141 */ 141 */
142 static BoolState from(bool value) => 142 static BoolState from(bool value) =>
143 value ? BoolState.TRUE_STATE : BoolState.FALSE_STATE; 143 value ? BoolState.TRUE_STATE : BoolState.FALSE_STATE;
144 } 144 }
145 145
146 /** 146 /**
147 * An [AstCloner] that copies the necessary information from the AST to allow 147 * An [AstCloner] that copies the necessary information from the AST to allow
148 * const constructor initializers to be evaluated. 148 * constants to be evaluated.
149 */ 149 */
150 class ConstantAstCloner extends AstCloner { 150 class ConstantAstCloner extends AstCloner {
151 ConstantAstCloner() : super(true); 151 ConstantAstCloner() : super(true);
152 152
153 @override 153 @override
154 InstanceCreationExpression visitInstanceCreationExpression( 154 InstanceCreationExpression visitInstanceCreationExpression(
155 InstanceCreationExpression node) { 155 InstanceCreationExpression node) {
156 InstanceCreationExpression expression = 156 InstanceCreationExpression expression =
157 super.visitInstanceCreationExpression(node); 157 super.visitInstanceCreationExpression(node);
158 expression.evaluationResult = node.evaluationResult; 158 expression.constantHandle = node.constantHandle;
159 return expression; 159 return expression;
160 } 160 }
161 161
162 @override 162 @override
163 RedirectingConstructorInvocation visitRedirectingConstructorInvocation( 163 RedirectingConstructorInvocation visitRedirectingConstructorInvocation(
164 RedirectingConstructorInvocation node) { 164 RedirectingConstructorInvocation node) {
165 RedirectingConstructorInvocation invocation = 165 RedirectingConstructorInvocation invocation =
166 super.visitRedirectingConstructorInvocation(node); 166 super.visitRedirectingConstructorInvocation(node);
167 invocation.staticElement = node.staticElement; 167 invocation.staticElement = node.staticElement;
168 return invocation; 168 return invocation;
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 * A visitor used to traverse the AST structures of all of the compilation units 275 * A visitor used to traverse the AST structures of all of the compilation units
276 * being resolved and build tables of the constant variables, constant 276 * being resolved and build tables of the constant variables, constant
277 * constructors, constant constructor invocations, and annotations found in 277 * constructors, constant constructor invocations, and annotations found in
278 * those compilation units. 278 * those compilation units.
279 */ 279 */
280 class ConstantFinder extends RecursiveAstVisitor<Object> { 280 class ConstantFinder extends RecursiveAstVisitor<Object> {
281 /** 281 /**
282 * A table mapping constant variable elements to the declarations of those 282 * A table mapping constant variable elements to the declarations of those
283 * variables. 283 * variables.
284 */ 284 */
285 final HashMap<VariableElement, VariableDeclaration> variableMap = 285 final HashMap<PotentiallyConstVariableElement, VariableDeclaration> variableMa p =
286 new HashMap<VariableElement, VariableDeclaration>(); 286 new HashMap<PotentiallyConstVariableElement, VariableDeclaration>();
287 287
288 /** 288 /**
289 * A table mapping constant constructors to the declarations of those 289 * A table mapping constant constructors to the declarations of those
290 * constructors. 290 * constructors.
291 */ 291 */
292 final HashMap<ConstructorElement, ConstructorDeclaration> constructorMap = 292 final HashMap<ConstructorElement, ConstructorDeclaration> constructorMap =
293 new HashMap<ConstructorElement, ConstructorDeclaration>(); 293 new HashMap<ConstructorElement, ConstructorDeclaration>();
294 294
295 /** 295 /**
296 * A collection of constant constructor invocations. 296 * A collection of constant constructor invocations.
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 constructorInvocations.add(node); 329 constructorInvocations.add(node);
330 } 330 }
331 return null; 331 return null;
332 } 332 }
333 333
334 @override 334 @override
335 Object visitVariableDeclaration(VariableDeclaration node) { 335 Object visitVariableDeclaration(VariableDeclaration node) {
336 super.visitVariableDeclaration(node); 336 super.visitVariableDeclaration(node);
337 Expression initializer = node.initializer; 337 Expression initializer = node.initializer;
338 if (initializer != null && node.isConst) { 338 if (initializer != null && node.isConst) {
339 VariableElement element = node.element; 339 if (node.element != null) {
340 if (element != null) { 340 variableMap[(node.element as PotentiallyConstVariableElement)] = node;
Brian Wilkerson 2015/04/02 15:58:59 nit: the parens shouldn't be necessary here
Paul Berry 2015/04/02 16:32:06 Fixed. I'm glad you noticed this, because those p
341 variableMap[element] = node;
342 } 341 }
343 } 342 }
344 return null; 343 return null;
345 } 344 }
346 } 345 }
347 346
348 /** 347 /**
349 * An object used to compute the values of constant variables and constant 348 * An object used to compute the values of constant variables and constant
350 * constructor invocations in one or more compilation units. The expected usage 349 * constructor invocations in one or more compilation units. The expected usage
351 * pattern is for the compilation units to be added to this computer using the 350 * pattern is for the compilation units to be added to this computer using the
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
400 399
401 /** 400 /**
402 * A graph in which the nodes are the constants, and the edges are from each 401 * A graph in which the nodes are the constants, and the edges are from each
403 * constant to the other constants that are referenced by it. 402 * constant to the other constants that are referenced by it.
404 */ 403 */
405 DirectedGraph<AstNode> referenceGraph = new DirectedGraph<AstNode>(); 404 DirectedGraph<AstNode> referenceGraph = new DirectedGraph<AstNode>();
406 405
407 /** 406 /**
408 * A table mapping constant variables to the declarations of those variables. 407 * A table mapping constant variables to the declarations of those variables.
409 */ 408 */
410 HashMap<VariableElement, VariableDeclaration> _variableDeclarationMap; 409 HashMap<PotentiallyConstVariableElement, VariableDeclaration> _variableDeclara tionMap;
411 410
412 /** 411 /**
413 * A table mapping constant constructors to the declarations of those 412 * A table mapping constant constructors to the declarations of those
414 * constructors. 413 * constructors.
415 */ 414 */
416 HashMap<ConstructorElement, ConstructorDeclaration> constructorDeclarationMap; 415 HashMap<ConstructorElement, ConstructorDeclaration> constructorDeclarationMap;
417 416
418 /** 417 /**
419 * A collection of constant constructor invocations. 418 * A collection of constant constructor invocations.
420 */ 419 */
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 _variableDeclarationMap = _constantFinder.variableMap; 474 _variableDeclarationMap = _constantFinder.variableMap;
476 constructorDeclarationMap = _constantFinder.constructorMap; 475 constructorDeclarationMap = _constantFinder.constructorMap;
477 _constructorInvocations = _constantFinder.constructorInvocations; 476 _constructorInvocations = _constantFinder.constructorInvocations;
478 _annotations = _constantFinder.annotations; 477 _annotations = _constantFinder.annotations;
479 _variableDeclarationMap.values.forEach((VariableDeclaration declaration) { 478 _variableDeclarationMap.values.forEach((VariableDeclaration declaration) {
480 ReferenceFinder referenceFinder = new ReferenceFinder(declaration, 479 ReferenceFinder referenceFinder = new ReferenceFinder(declaration,
481 referenceGraph, _variableDeclarationMap, constructorDeclarationMap); 480 referenceGraph, _variableDeclarationMap, constructorDeclarationMap);
482 referenceGraph.addNode(declaration); 481 referenceGraph.addNode(declaration);
483 declaration.initializer.accept(referenceFinder); 482 declaration.initializer.accept(referenceFinder);
484 }); 483 });
485 constructorDeclarationMap.forEach((ConstructorElement element, 484 constructorDeclarationMap.forEach((ConstructorElementImpl element,
486 ConstructorDeclaration declaration) { 485 ConstructorDeclaration declaration) {
486 element.isCycleFree = false;
487 ConstructorElement redirectedConstructor = 487 ConstructorElement redirectedConstructor =
488 _getConstRedirectedConstructor(element); 488 _getConstRedirectedConstructor(element);
489 if (redirectedConstructor != null) { 489 if (redirectedConstructor != null) {
490 ConstructorElement redirectedConstructorBase = 490 ConstructorElement redirectedConstructorBase =
491 _getConstructorBase(redirectedConstructor); 491 _getConstructorBase(redirectedConstructor);
492 ConstructorDeclaration redirectedConstructorDeclaration = 492 ConstructorDeclaration redirectedConstructorDeclaration =
493 findConstructorDeclaration(redirectedConstructorBase); 493 findConstructorDeclaration(redirectedConstructorBase);
494 referenceGraph.addEdge(declaration, redirectedConstructorDeclaration); 494 referenceGraph.addEdge(declaration, redirectedConstructorDeclaration);
495 return; 495 return;
496 } 496 }
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
644 String name = argumentValues[0].stringValue; 644 String name = argumentValues[0].stringValue;
645 return isValidPublicSymbol(name); 645 return isValidPublicSymbol(name);
646 } 646 }
647 647
648 /** 648 /**
649 * Compute a value for the given [constNode]. 649 * Compute a value for the given [constNode].
650 */ 650 */
651 void _computeValueFor(AstNode constNode) { 651 void _computeValueFor(AstNode constNode) {
652 beforeComputeValue(constNode); 652 beforeComputeValue(constNode);
653 if (constNode is VariableDeclaration) { 653 if (constNode is VariableDeclaration) {
654 VariableDeclaration declaration = constNode; 654 VariableElement element = constNode.element;
655 VariableElement element = declaration.element;
656 RecordingErrorListener errorListener = new RecordingErrorListener(); 655 RecordingErrorListener errorListener = new RecordingErrorListener();
657 ErrorReporter errorReporter = 656 ErrorReporter errorReporter =
658 new ErrorReporter(errorListener, element.source); 657 new ErrorReporter(errorListener, element.source);
659 DartObjectImpl dartObject = 658 DartObjectImpl dartObject =
660 declaration.initializer.accept(createConstantVisitor(errorReporter)); 659 (element as PotentiallyConstVariableElement).constantInitializer
660 .accept(createConstantVisitor(errorReporter));
661 if (dartObject != null) { 661 if (dartObject != null) {
662 if (!_runtimeTypeMatch(dartObject, element.type)) { 662 if (!_runtimeTypeMatch(dartObject, element.type)) {
663 errorReporter.reportErrorForNode( 663 errorReporter.reportErrorForElement(
664 CheckedModeCompileTimeErrorCode.VARIABLE_TYPE_MISMATCH, 664 CheckedModeCompileTimeErrorCode.VARIABLE_TYPE_MISMATCH, element, [
665 declaration, [dartObject.type, element.type]); 665 dartObject.type,
666 element.type
667 ]);
666 } 668 }
667 } 669 }
668 (element as VariableElementImpl).evaluationResult = 670 (element as VariableElementImpl).evaluationResult =
669 new EvaluationResultImpl.con2(dartObject, errorListener.errors); 671 new EvaluationResultImpl.con2(dartObject, errorListener.errors);
670 } else if (constNode is InstanceCreationExpression) { 672 } else if (constNode is InstanceCreationExpression) {
671 InstanceCreationExpression expression = constNode; 673 InstanceCreationExpression expression = constNode;
672 ConstructorElement constructor = expression.staticElement; 674 ConstructorElement constructor = expression.staticElement;
673 if (constructor == null) { 675 if (constructor == null) {
674 // Couldn't resolve the constructor so we can't compute a value. 676 // Couldn't resolve the constructor so we can't compute a value.
675 // No problem - the error has already been reported. 677 // No problem - the error has already been reported.
676 // But we still need to store an evaluation result. 678 // But we still need to store an evaluation result.
677 expression.evaluationResult = new EvaluationResultImpl.con1(null); 679 expression.constantHandle.evaluationResult =
680 new EvaluationResultImpl.con1(null);
678 return; 681 return;
679 } 682 }
680 RecordingErrorListener errorListener = new RecordingErrorListener(); 683 RecordingErrorListener errorListener = new RecordingErrorListener();
681 CompilationUnit sourceCompilationUnit = 684 CompilationUnit sourceCompilationUnit =
682 expression.getAncestor((node) => node is CompilationUnit); 685 expression.getAncestor((node) => node is CompilationUnit);
683 ErrorReporter errorReporter = new ErrorReporter( 686 ErrorReporter errorReporter = new ErrorReporter(
684 errorListener, sourceCompilationUnit.element.source); 687 errorListener, sourceCompilationUnit.element.source);
685 ConstantVisitor constantVisitor = createConstantVisitor(errorReporter); 688 ConstantVisitor constantVisitor = createConstantVisitor(errorReporter);
686 DartObjectImpl result = _evaluateConstructorCall(constNode, 689 DartObjectImpl result = _evaluateConstructorCall(constNode,
687 expression.argumentList.arguments, constructor, constantVisitor, 690 expression.argumentList.arguments, constructor, constantVisitor,
688 errorReporter); 691 errorReporter);
689 expression.evaluationResult = 692 expression.constantHandle.evaluationResult =
690 new EvaluationResultImpl.con2(result, errorListener.errors); 693 new EvaluationResultImpl.con2(result, errorListener.errors);
691 } else if (constNode is ConstructorDeclaration) { 694 } else if (constNode is ConstructorDeclaration) {
692 ConstructorDeclaration declaration = constNode; 695 // No evaluation needs to be done; constructor declarations are only in
693 NodeList<ConstructorInitializer> initializers = declaration.initializers; 696 // the dependency graph to ensure that any constants referred to in
694 ConstructorElementImpl constructor = 697 // initializer lists and parameter defaults are evaluated before
695 declaration.element as ConstructorElementImpl; 698 // invocations of the constructor. However we do need to annotate the
696 constructor.constantInitializers = 699 // element as being free of constant evaluation cycles so that later code
697 new ConstantAstCloner().cloneNodeList(initializers); 700 // will know that it is safe to evaluate.
701 ConstructorElementImpl constructor = constNode.element;
702 constructor.isCycleFree = true;
698 } else if (constNode is FormalParameter) { 703 } else if (constNode is FormalParameter) {
699 if (constNode is DefaultFormalParameter) { 704 if (constNode is DefaultFormalParameter) {
700 DefaultFormalParameter parameter = constNode; 705 DefaultFormalParameter parameter = constNode;
701 ParameterElement element = parameter.element; 706 ParameterElement element = parameter.element;
702 Expression defaultValue = parameter.defaultValue; 707 Expression defaultValue = parameter.defaultValue;
703 if (defaultValue != null) { 708 if (defaultValue != null) {
704 RecordingErrorListener errorListener = new RecordingErrorListener(); 709 RecordingErrorListener errorListener = new RecordingErrorListener();
705 ErrorReporter errorReporter = 710 ErrorReporter errorReporter =
706 new ErrorReporter(errorListener, element.source); 711 new ErrorReporter(errorListener, element.source);
707 DartObjectImpl dartObject = 712 DartObjectImpl dartObject =
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
785 // that for analysis because it's likely to lead to cascading errors. 790 // that for analysis because it's likely to lead to cascading errors.
786 // So just leave [value] in the unknown state. 791 // So just leave [value] in the unknown state.
787 } 792 }
788 } 793 }
789 return value; 794 return value;
790 } 795 }
791 796
792 DartObjectImpl _evaluateConstructorCall(AstNode node, 797 DartObjectImpl _evaluateConstructorCall(AstNode node,
793 NodeList<Expression> arguments, ConstructorElement constructor, 798 NodeList<Expression> arguments, ConstructorElement constructor,
794 ConstantVisitor constantVisitor, ErrorReporter errorReporter) { 799 ConstantVisitor constantVisitor, ErrorReporter errorReporter) {
800 if (!_getConstructorBase(constructor).isCycleFree) {
801 // It's not safe to evaluate this constructor, so bail out.
802 // TODO(paulberry): ensure that a reasonable error message is produced
803 // in this case, as well as other cases involving constant expression
804 // circularities (e.g. "compile-time constant expression depends on
805 // itself")
806 return new DartObjectImpl.validWithUnknownValue(constructor.returnType);
807 }
795 int argumentCount = arguments.length; 808 int argumentCount = arguments.length;
796 List<DartObjectImpl> argumentValues = 809 List<DartObjectImpl> argumentValues =
797 new List<DartObjectImpl>(argumentCount); 810 new List<DartObjectImpl>(argumentCount);
798 List<Expression> argumentNodes = new List<Expression>(argumentCount); 811 List<Expression> argumentNodes = new List<Expression>(argumentCount);
799 HashMap<String, DartObjectImpl> namedArgumentValues = 812 HashMap<String, DartObjectImpl> namedArgumentValues =
800 new HashMap<String, DartObjectImpl>(); 813 new HashMap<String, DartObjectImpl>();
801 HashMap<String, NamedExpression> namedArgumentNodes = 814 HashMap<String, NamedExpression> namedArgumentNodes =
802 new HashMap<String, NamedExpression>(); 815 new HashMap<String, NamedExpression>();
803 for (int i = 0; i < argumentCount; i++) { 816 for (int i = 0; i < argumentCount; i++) {
804 Expression argument = arguments[i]; 817 Expression argument = arguments[i];
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
862 String argumentValue = argumentValues[0].stringValue; 875 String argumentValue = argumentValues[0].stringValue;
863 return new DartObjectImpl( 876 return new DartObjectImpl(
864 definingClass, new SymbolState(argumentValue)); 877 definingClass, new SymbolState(argumentValue));
865 } 878 }
866 // Either it's an external const factory constructor that we can't 879 // Either it's an external const factory constructor that we can't
867 // emulate, or an error occurred (a cycle, or a const constructor trying 880 // emulate, or an error occurred (a cycle, or a const constructor trying
868 // to delegate to a non-const constructor). 881 // to delegate to a non-const constructor).
869 // In the former case, the best we can do is consider it an unknown value. 882 // In the former case, the best we can do is consider it an unknown value.
870 // In the latter case, the error has already been reported, so considering 883 // In the latter case, the error has already been reported, so considering
871 // it an unknown value will suppress further errors. 884 // it an unknown value will suppress further errors.
872 return constantVisitor._validWithUnknownValue(definingClass); 885 return new DartObjectImpl.validWithUnknownValue(definingClass);
873 } 886 }
874 beforeGetConstantInitializers(constructor); 887 beforeGetConstantInitializers(constructor);
875 ConstructorElementImpl constructorBase = 888 ConstructorElementImpl constructorBase = _getConstructorBase(constructor);
876 _getConstructorBase(constructor) as ConstructorElementImpl;
877 List<ConstructorInitializer> initializers = 889 List<ConstructorInitializer> initializers =
878 constructorBase.constantInitializers; 890 constructorBase.constantInitializers;
879 if (initializers == null) { 891 if (initializers == null) {
880 // This can happen in some cases where there are compile errors in the 892 // This can happen in some cases where there are compile errors in the
881 // code being analyzed (for example if the code is trying to create a 893 // code being analyzed (for example if the code is trying to create a
882 // const instance using a non-const constructor, or the node we're 894 // const instance using a non-const constructor, or the node we're
883 // visiting is involved in a cycle). The error has already been reported, 895 // visiting is involved in a cycle). The error has already been reported,
884 // so consider it an unknown value to suppress further errors. 896 // so consider it an unknown value to suppress further errors.
885 return constantVisitor._validWithUnknownValue(definingClass); 897 return new DartObjectImpl.validWithUnknownValue(definingClass);
886 } 898 }
887 HashMap<String, DartObjectImpl> fieldMap = 899 HashMap<String, DartObjectImpl> fieldMap =
888 new HashMap<String, DartObjectImpl>(); 900 new HashMap<String, DartObjectImpl>();
889 HashMap<String, DartObjectImpl> parameterMap = 901 HashMap<String, DartObjectImpl> parameterMap =
890 new HashMap<String, DartObjectImpl>(); 902 new HashMap<String, DartObjectImpl>();
891 List<ParameterElement> parameters = constructor.parameters; 903 List<ParameterElement> parameters = constructor.parameters;
892 int parameterCount = parameters.length; 904 int parameterCount = parameters.length;
893 for (int i = 0; i < parameterCount; i++) { 905 for (int i = 0; i < parameterCount; i++) {
894 ParameterElement parameter = parameters[i]; 906 ParameterElement parameter = parameters[i];
895 ParameterElement baseParameter = parameter; 907 ParameterElement baseParameter = parameter;
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
1092 } 1104 }
1093 if (!redirectedConstructor.isConst) { 1105 if (!redirectedConstructor.isConst) {
1094 // Delegating to a non-const constructor--this is not allowed (and 1106 // Delegating to a non-const constructor--this is not allowed (and
1095 // is checked elsewhere--see 1107 // is checked elsewhere--see
1096 // [ErrorVerifier.checkForRedirectToNonConstConstructor()]). 1108 // [ErrorVerifier.checkForRedirectToNonConstConstructor()]).
1097 return null; 1109 return null;
1098 } 1110 }
1099 return redirectedConstructor; 1111 return redirectedConstructor;
1100 } 1112 }
1101 1113
1102 ConstructorElement _getConstructorBase(ConstructorElement constructor) { 1114 ConstructorElementImpl _getConstructorBase(ConstructorElement constructor) {
1103 while (constructor is ConstructorMember) { 1115 while (constructor is ConstructorMember) {
1104 constructor = (constructor as ConstructorMember).baseElement; 1116 constructor = (constructor as ConstructorMember).baseElement;
1105 } 1117 }
1106 return constructor; 1118 return constructor;
1107 } 1119 }
1108 1120
1109 /** 1121 /**
1110 * Check if the object [obj] matches the type [type] according to runtime type 1122 * Check if the object [obj] matches the type [type] according to runtime type
1111 * checking rules. 1123 * checking rules.
1112 */ 1124 */
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
1352 if (conditionResult == null) { 1364 if (conditionResult == null) {
1353 return conditionResult; 1365 return conditionResult;
1354 } 1366 }
1355 if (conditionResult.isTrue) { 1367 if (conditionResult.isTrue) {
1356 return thenResult; 1368 return thenResult;
1357 } else if (conditionResult.isFalse) { 1369 } else if (conditionResult.isFalse) {
1358 return elseResult; 1370 return elseResult;
1359 } 1371 }
1360 ParameterizedType thenType = thenResult.type; 1372 ParameterizedType thenType = thenResult.type;
1361 ParameterizedType elseType = elseResult.type; 1373 ParameterizedType elseType = elseResult.type;
1362 return _validWithUnknownValue( 1374 return new DartObjectImpl.validWithUnknownValue(
1363 thenType.getLeastUpperBound(elseType) as InterfaceType); 1375 thenType.getLeastUpperBound(elseType) as InterfaceType);
1364 } 1376 }
1365 1377
1366 @override 1378 @override
1367 DartObjectImpl visitDoubleLiteral(DoubleLiteral node) => 1379 DartObjectImpl visitDoubleLiteral(DoubleLiteral node) =>
1368 new DartObjectImpl(_typeProvider.doubleType, new DoubleState(node.value)); 1380 new DartObjectImpl(_typeProvider.doubleType, new DoubleState(node.value));
1369 1381
1370 @override 1382 @override
1371 DartObjectImpl visitInstanceCreationExpression( 1383 DartObjectImpl visitInstanceCreationExpression(
1372 InstanceCreationExpression node) { 1384 InstanceCreationExpression node) {
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
1656 } else if (element is ClassElement || 1668 } else if (element is ClassElement ||
1657 element is FunctionTypeAliasElement || 1669 element is FunctionTypeAliasElement ||
1658 element is DynamicElementImpl) { 1670 element is DynamicElementImpl) {
1659 return new DartObjectImpl(_typeProvider.typeType, new TypeState(element)); 1671 return new DartObjectImpl(_typeProvider.typeType, new TypeState(element));
1660 } 1672 }
1661 // TODO(brianwilkerson) Figure out which error to report. 1673 // TODO(brianwilkerson) Figure out which error to report.
1662 _error(node, null); 1674 _error(node, null);
1663 return null; 1675 return null;
1664 } 1676 }
1665 1677
1666 DartObjectImpl _validWithUnknownValue(InterfaceType type) {
1667 if (type.element.library.isDartCore) {
1668 String typeName = type.name;
1669 if (typeName == "bool") {
1670 return new DartObjectImpl(type, BoolState.UNKNOWN_VALUE);
1671 } else if (typeName == "double") {
1672 return new DartObjectImpl(type, DoubleState.UNKNOWN_VALUE);
1673 } else if (typeName == "int") {
1674 return new DartObjectImpl(type, IntState.UNKNOWN_VALUE);
1675 } else if (typeName == "String") {
1676 return new DartObjectImpl(type, StringState.UNKNOWN_VALUE);
1677 }
1678 }
1679 return new DartObjectImpl(type, GenericState.UNKNOWN_VALUE);
1680 }
1681
1682 /** 1678 /**
1683 * Return the value of the given [expression], or a representation of 'null' 1679 * Return the value of the given [expression], or a representation of 'null'
1684 * if the expression cannot be evaluated. 1680 * if the expression cannot be evaluated.
1685 */ 1681 */
1686 DartObjectImpl _valueOf(Expression expression) { 1682 DartObjectImpl _valueOf(Expression expression) {
1687 DartObjectImpl expressionValue = expression.accept(this); 1683 DartObjectImpl expressionValue = expression.accept(this);
1688 if (expressionValue != null) { 1684 if (expressionValue != null) {
1689 return expressionValue; 1685 return expressionValue;
1690 } 1686 }
1691 return _typeProvider.nullObject; 1687 return _typeProvider.nullObject;
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after
2121 /** 2117 /**
2122 * The state of the object. 2118 * The state of the object.
2123 */ 2119 */
2124 final InstanceState _state; 2120 final InstanceState _state;
2125 2121
2126 /** 2122 /**
2127 * Initialize a newly created object to have the given [type] and [_state]. 2123 * Initialize a newly created object to have the given [type] and [_state].
2128 */ 2124 */
2129 DartObjectImpl(this.type, this._state); 2125 DartObjectImpl(this.type, this._state);
2130 2126
2127 /**
2128 * Create an object to represent an unknown value.
2129 */
2130 factory DartObjectImpl.validWithUnknownValue(InterfaceType type) {
2131 if (type.element.library.isDartCore) {
2132 String typeName = type.name;
2133 if (typeName == "bool") {
2134 return new DartObjectImpl(type, BoolState.UNKNOWN_VALUE);
2135 } else if (typeName == "double") {
2136 return new DartObjectImpl(type, DoubleState.UNKNOWN_VALUE);
2137 } else if (typeName == "int") {
2138 return new DartObjectImpl(type, IntState.UNKNOWN_VALUE);
2139 } else if (typeName == "String") {
2140 return new DartObjectImpl(type, StringState.UNKNOWN_VALUE);
2141 }
2142 }
2143 return new DartObjectImpl(type, GenericState.UNKNOWN_VALUE);
2144 }
2145
2131 @override 2146 @override
2132 bool get boolValue { 2147 bool get boolValue {
2133 if (_state is BoolState) { 2148 if (_state is BoolState) {
2134 return (_state as BoolState).value; 2149 return (_state as BoolState).value;
2135 } 2150 }
2136 return null; 2151 return null;
2137 } 2152 }
2138 2153
2139 @override 2154 @override
2140 double get doubleValue { 2155 double get doubleValue {
(...skipping 2643 matching lines...) Expand 10 before | Expand all | Expand 10 after
4784 /** 4799 /**
4785 * A graph in which the nodes are the constant variables and the edges are 4800 * A graph in which the nodes are the constant variables and the edges are
4786 * from each variable to the other constant variables that are referenced in 4801 * from each variable to the other constant variables that are referenced in
4787 * the head's initializer. 4802 * the head's initializer.
4788 */ 4803 */
4789 final DirectedGraph<AstNode> _referenceGraph; 4804 final DirectedGraph<AstNode> _referenceGraph;
4790 4805
4791 /** 4806 /**
4792 * A table mapping constant variables to the declarations of those variables. 4807 * A table mapping constant variables to the declarations of those variables.
4793 */ 4808 */
4794 final HashMap<VariableElement, VariableDeclaration> _variableDeclarationMap; 4809 final HashMap<PotentiallyConstVariableElement, VariableDeclaration> _variableD eclarationMap;
4795 4810
4796 /** 4811 /**
4797 * A table mapping constant constructors to the declarations of those 4812 * A table mapping constant constructors to the declarations of those
4798 * constructors. 4813 * constructors.
4799 */ 4814 */
4800 final HashMap<ConstructorElement, ConstructorDeclaration> _constructorDeclarat ionMap; 4815 final HashMap<ConstructorElement, ConstructorDeclaration> _constructorDeclarat ionMap;
4801 4816
4802 /** 4817 /**
4803 * Initialize a newly created reference finder to find references from a given 4818 * Initialize a newly created reference finder to find references from a given
4804 * variable to other variables and to add those references to the given graph. 4819 * variable to other variables and to add those references to the given graph.
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after
5087 return BoolState.from(_element == rightElement); 5102 return BoolState.from(_element == rightElement);
5088 } else if (rightOperand is DynamicState) { 5103 } else if (rightOperand is DynamicState) {
5089 return BoolState.UNKNOWN_VALUE; 5104 return BoolState.UNKNOWN_VALUE;
5090 } 5105 }
5091 return BoolState.FALSE_STATE; 5106 return BoolState.FALSE_STATE;
5092 } 5107 }
5093 5108
5094 @override 5109 @override
5095 String toString() => _element == null ? "-unknown-" : _element.name; 5110 String toString() => _element == null ? "-unknown-" : _element.name;
5096 } 5111 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/generated/ast.dart ('k') | pkg/analyzer/lib/src/generated/element.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698