| OLD | NEW |
| 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 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 DartObjectImpl result = | 221 DartObjectImpl result = |
| 222 expression.accept(new ConstantVisitor.con1(_typeProvider, errorReporter)
); | 222 expression.accept(new ConstantVisitor.con1(_typeProvider, errorReporter)
); |
| 223 if (result != null) { | 223 if (result != null) { |
| 224 return EvaluationResult.forValue(result); | 224 return EvaluationResult.forValue(result); |
| 225 } | 225 } |
| 226 return EvaluationResult.forErrors(errorListener.errors); | 226 return EvaluationResult.forErrors(errorListener.errors); |
| 227 } | 227 } |
| 228 } | 228 } |
| 229 | 229 |
| 230 /** | 230 /** |
| 231 * Instances of the class `ConstantFinder` are used to traverse the AST structur
es of all of | 231 * Instances of the class `ConstantFinder` are used to traverse the AST |
| 232 * the compilation units being resolved and build a table mapping constant varia
ble elements to the | 232 * structures of all of the compilation units being resolved and build tables |
| 233 * declarations of those variables. | 233 * of the constant variables, constant constructors, constant constructor |
| 234 * invocations, and annotations found in those compilation units. |
| 234 */ | 235 */ |
| 235 class ConstantFinder extends RecursiveAstVisitor<Object> { | 236 class ConstantFinder extends RecursiveAstVisitor<Object> { |
| 236 /** | 237 /** |
| 237 * A table mapping constant variable elements to the declarations of those var
iables. | 238 * A table mapping constant variable elements to the declarations of those var
iables. |
| 238 */ | 239 */ |
| 239 final HashMap<VariableElement, VariableDeclaration> variableMap = | 240 final HashMap<VariableElement, VariableDeclaration> variableMap = |
| 240 new HashMap<VariableElement, VariableDeclaration>(); | 241 new HashMap<VariableElement, VariableDeclaration>(); |
| 241 | 242 |
| 242 /** | 243 /** |
| 243 * A table mapping constant constructors to the declarations of those construc
tors. | 244 * A table mapping constant constructors to the declarations of those construc
tors. |
| 244 */ | 245 */ |
| 245 final HashMap<ConstructorElement, ConstructorDeclaration> constructorMap = | 246 final HashMap<ConstructorElement, ConstructorDeclaration> constructorMap = |
| 246 new HashMap<ConstructorElement, ConstructorDeclaration>(); | 247 new HashMap<ConstructorElement, ConstructorDeclaration>(); |
| 247 | 248 |
| 248 /** | 249 /** |
| 249 * A collection of constant constructor invocations. | 250 * A collection of constant constructor invocations. |
| 250 */ | 251 */ |
| 251 final List<InstanceCreationExpression> constructorInvocations = | 252 final List<InstanceCreationExpression> constructorInvocations = |
| 252 new List<InstanceCreationExpression>(); | 253 new List<InstanceCreationExpression>(); |
| 253 | 254 |
| 255 /** |
| 256 * A collection of annotations. |
| 257 */ |
| 258 final List<Annotation> annotations = <Annotation>[]; |
| 259 |
| 260 @override |
| 261 Object visitAnnotation(Annotation node) { |
| 262 super.visitAnnotation(node); |
| 263 annotations.add(node); |
| 264 return null; |
| 265 } |
| 266 |
| 254 @override | 267 @override |
| 255 Object visitConstructorDeclaration(ConstructorDeclaration node) { | 268 Object visitConstructorDeclaration(ConstructorDeclaration node) { |
| 256 super.visitConstructorDeclaration(node); | 269 super.visitConstructorDeclaration(node); |
| 257 if (node.constKeyword != null) { | 270 if (node.constKeyword != null) { |
| 258 ConstructorElement element = node.element; | 271 ConstructorElement element = node.element; |
| 259 if (element != null) { | 272 if (element != null) { |
| 260 constructorMap[element] = node; | 273 constructorMap[element] = node; |
| 261 } | 274 } |
| 262 } | 275 } |
| 263 return null; | 276 return null; |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 * A table mapping constant constructors to the declarations of those construc
tors. | 363 * A table mapping constant constructors to the declarations of those construc
tors. |
| 351 */ | 364 */ |
| 352 HashMap<ConstructorElement, ConstructorDeclaration> constructorDeclarationMap; | 365 HashMap<ConstructorElement, ConstructorDeclaration> constructorDeclarationMap; |
| 353 | 366 |
| 354 /** | 367 /** |
| 355 * A collection of constant constructor invocations. | 368 * A collection of constant constructor invocations. |
| 356 */ | 369 */ |
| 357 List<InstanceCreationExpression> _constructorInvocations; | 370 List<InstanceCreationExpression> _constructorInvocations; |
| 358 | 371 |
| 359 /** | 372 /** |
| 373 * A collection of annotations. |
| 374 */ |
| 375 List<Annotation> _annotations; |
| 376 |
| 377 /** |
| 360 * The set of variables declared on the command line using '-D'. | 378 * The set of variables declared on the command line using '-D'. |
| 361 */ | 379 */ |
| 362 final DeclaredVariables _declaredVariables; | 380 final DeclaredVariables _declaredVariables; |
| 363 | 381 |
| 364 /** | 382 /** |
| 365 * Initialize a newly created constant value computer. | 383 * Initialize a newly created constant value computer. |
| 366 * | 384 * |
| 367 * @param typeProvider the type provider used to access known types | 385 * @param typeProvider the type provider used to access known types |
| 368 * @param declaredVariables the set of variables declared on the command line
using '-D' | 386 * @param declaredVariables the set of variables declared on the command line
using '-D' |
| 369 */ | 387 */ |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 void beforeGetParameterDefault(ParameterElement parameter) { | 421 void beforeGetParameterDefault(ParameterElement parameter) { |
| 404 } | 422 } |
| 405 | 423 |
| 406 /** | 424 /** |
| 407 * Compute values for all of the constants in the compilation units that were
added. | 425 * Compute values for all of the constants in the compilation units that were
added. |
| 408 */ | 426 */ |
| 409 void computeValues() { | 427 void computeValues() { |
| 410 _variableDeclarationMap = _constantFinder.variableMap; | 428 _variableDeclarationMap = _constantFinder.variableMap; |
| 411 constructorDeclarationMap = _constantFinder.constructorMap; | 429 constructorDeclarationMap = _constantFinder.constructorMap; |
| 412 _constructorInvocations = _constantFinder.constructorInvocations; | 430 _constructorInvocations = _constantFinder.constructorInvocations; |
| 431 _annotations = _constantFinder.annotations; |
| 413 _variableDeclarationMap.values.forEach((VariableDeclaration declaration) { | 432 _variableDeclarationMap.values.forEach((VariableDeclaration declaration) { |
| 414 ReferenceFinder referenceFinder = new ReferenceFinder( | 433 ReferenceFinder referenceFinder = new ReferenceFinder( |
| 415 declaration, | 434 declaration, |
| 416 referenceGraph, | 435 referenceGraph, |
| 417 _variableDeclarationMap, | 436 _variableDeclarationMap, |
| 418 constructorDeclarationMap); | 437 constructorDeclarationMap); |
| 419 referenceGraph.addNode(declaration); | 438 referenceGraph.addNode(declaration); |
| 420 declaration.initializer.accept(referenceFinder); | 439 declaration.initializer.accept(referenceFinder); |
| 421 }); | 440 }); |
| 422 constructorDeclarationMap.forEach( | 441 constructorDeclarationMap.forEach( |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 referenceGraph.computeTopologicalSort(); | 510 referenceGraph.computeTopologicalSort(); |
| 492 for (List<AstNode> constantsInCycle in topologicalSort) { | 511 for (List<AstNode> constantsInCycle in topologicalSort) { |
| 493 if (constantsInCycle.length == 1) { | 512 if (constantsInCycle.length == 1) { |
| 494 _computeValueFor(constantsInCycle[0]); | 513 _computeValueFor(constantsInCycle[0]); |
| 495 } else { | 514 } else { |
| 496 for (AstNode constant in constantsInCycle) { | 515 for (AstNode constant in constantsInCycle) { |
| 497 _generateCycleError(constantsInCycle, constant); | 516 _generateCycleError(constantsInCycle, constant); |
| 498 } | 517 } |
| 499 } | 518 } |
| 500 } | 519 } |
| 520 // Since no constant can depend on an annotation, we don't waste time |
| 521 // including them in the topological sort. We just process all the |
| 522 // annotations after all other constants are finished. |
| 523 for (Annotation annotation in _annotations) { |
| 524 _computeValueFor(annotation); |
| 525 } |
| 501 } | 526 } |
| 502 | 527 |
| 503 /** | 528 /** |
| 504 * Create the ConstantVisitor used to evaluate constants. Unit tests will over
ride this method to | 529 * Create the ConstantVisitor used to evaluate constants. Unit tests will over
ride this method to |
| 505 * introduce additional error checking. | 530 * introduce additional error checking. |
| 506 */ | 531 */ |
| 507 ConstantVisitor createConstantVisitor(ErrorReporter errorReporter) => | 532 ConstantVisitor createConstantVisitor(ErrorReporter errorReporter) => |
| 508 new ConstantVisitor.con1(typeProvider, errorReporter); | 533 new ConstantVisitor.con1(typeProvider, errorReporter); |
| 509 | 534 |
| 510 ConstructorDeclaration | 535 ConstructorDeclaration |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 640 if (defaultValue != null) { | 665 if (defaultValue != null) { |
| 641 RecordingErrorListener errorListener = new RecordingErrorListener(); | 666 RecordingErrorListener errorListener = new RecordingErrorListener(); |
| 642 ErrorReporter errorReporter = | 667 ErrorReporter errorReporter = |
| 643 new ErrorReporter(errorListener, element.source); | 668 new ErrorReporter(errorListener, element.source); |
| 644 DartObjectImpl dartObject = | 669 DartObjectImpl dartObject = |
| 645 defaultValue.accept(createConstantVisitor(errorReporter)); | 670 defaultValue.accept(createConstantVisitor(errorReporter)); |
| 646 (element as ParameterElementImpl).evaluationResult = | 671 (element as ParameterElementImpl).evaluationResult = |
| 647 new EvaluationResultImpl.con2(dartObject, errorListener.errors); | 672 new EvaluationResultImpl.con2(dartObject, errorListener.errors); |
| 648 } | 673 } |
| 649 } | 674 } |
| 675 } else if (constNode is Annotation) { |
| 676 ElementAnnotationImpl elementAnnotation = constNode.elementAnnotation; |
| 677 // elementAnnotation is null if the annotation couldn't be resolved, in |
| 678 // which case we skip it. |
| 679 if (elementAnnotation != null) { |
| 680 Element element = elementAnnotation.element; |
| 681 if (element is PropertyAccessorElement && |
| 682 element.variable is VariableElementImpl) { |
| 683 // The annotation is a reference to a compile-time constant variable. |
| 684 // Just copy the evaluation result. |
| 685 VariableElementImpl variableElement = |
| 686 element.variable as VariableElementImpl; |
| 687 elementAnnotation.evaluationResult = variableElement.evaluationResult; |
| 688 } else if (element is ConstructorElementImpl && |
| 689 constNode.arguments != null) { |
| 690 RecordingErrorListener errorListener = new RecordingErrorListener(); |
| 691 CompilationUnit sourceCompilationUnit = |
| 692 constNode.getAncestor((node) => node is CompilationUnit); |
| 693 ErrorReporter errorReporter = |
| 694 new ErrorReporter(errorListener, sourceCompilationUnit.element.sou
rce); |
| 695 ConstantVisitor constantVisitor = |
| 696 createConstantVisitor(errorReporter); |
| 697 DartObjectImpl result = _evaluateConstructorCall( |
| 698 constNode, |
| 699 constNode.arguments.arguments, |
| 700 element, |
| 701 constantVisitor, |
| 702 errorReporter); |
| 703 elementAnnotation.evaluationResult = |
| 704 new EvaluationResultImpl.con2(result, errorListener.errors); |
| 705 } else { |
| 706 // This may happen for invalid code (e.g. failing to pass arguments |
| 707 // to an annotation which references a const constructor). The error |
| 708 // is detected elsewhere, so just silently ignore it here. |
| 709 elementAnnotation.evaluationResult = |
| 710 new EvaluationResultImpl.con1(null); |
| 711 } |
| 712 } |
| 650 } else { | 713 } else { |
| 651 // Should not happen. | 714 // Should not happen. |
| 652 AnalysisEngine.instance.logger.logError( | 715 AnalysisEngine.instance.logger.logError( |
| 653 "Constant value computer trying to compute the value of a node which i
s not a VariableDeclaration, InstanceCreationExpression, FormalParameter, or Con
structorDeclaration"); | 716 "Constant value computer trying to compute the value of a node which i
s not a VariableDeclaration, InstanceCreationExpression, FormalParameter, or Con
structorDeclaration"); |
| 654 return; | 717 return; |
| 655 } | 718 } |
| 656 } | 719 } |
| 657 | 720 |
| 658 /** | 721 /** |
| 659 * Evaluate a call to fromEnvironment() on the bool, int, or String class. | 722 * Evaluate a call to fromEnvironment() on the bool, int, or String class. |
| (...skipping 4464 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5124 return BoolState.from(_element == rightElement); | 5187 return BoolState.from(_element == rightElement); |
| 5125 } else if (rightOperand is DynamicState) { | 5188 } else if (rightOperand is DynamicState) { |
| 5126 return BoolState.UNKNOWN_VALUE; | 5189 return BoolState.UNKNOWN_VALUE; |
| 5127 } | 5190 } |
| 5128 return BoolState.FALSE_STATE; | 5191 return BoolState.FALSE_STATE; |
| 5129 } | 5192 } |
| 5130 | 5193 |
| 5131 @override | 5194 @override |
| 5132 String toString() => _element == null ? "-unknown-" : _element.name; | 5195 String toString() => _element == null ? "-unknown-" : _element.name; |
| 5133 } | 5196 } |
| OLD | NEW |