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

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

Issue 1329743005: Abstract over the type system. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 3 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) 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:collection'; 7 import 'dart:collection';
8 8
9 import 'ast.dart'; 9 import 'ast.dart';
10 import 'constant.dart'; 10 import 'constant.dart';
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 */ 66 */
67 final ErrorReporter _errorReporter; 67 final ErrorReporter _errorReporter;
68 68
69 /** 69 /**
70 * The type Future<Null>, which is needed for determining whether it is safe 70 * The type Future<Null>, which is needed for determining whether it is safe
71 * to have a bare "return;" in an async method. 71 * to have a bare "return;" in an async method.
72 */ 72 */
73 final InterfaceType _futureNullType; 73 final InterfaceType _futureNullType;
74 74
75 /** 75 /**
76 * The type system primitives
77 */
78 TypeSystem _typeSystem;
79
80 /**
76 * Create a new instance of the [BestPracticesVerifier]. 81 * Create a new instance of the [BestPracticesVerifier].
77 * 82 *
78 * @param errorReporter the error reporter 83 * @param errorReporter the error reporter
79 */ 84 */
80 BestPracticesVerifier(this._errorReporter, TypeProvider typeProvider) 85 BestPracticesVerifier(this._errorReporter, TypeProvider typeProvider, this._ty peSystem)
81 : _futureNullType = typeProvider.futureNullType; 86 : _futureNullType = typeProvider.futureNullType;
82 87
83 @override 88 @override
84 Object visitArgumentList(ArgumentList node) { 89 Object visitArgumentList(ArgumentList node) {
85 _checkForArgumentTypesNotAssignableInList(node); 90 _checkForArgumentTypesNotAssignableInList(node);
86 return super.visitArgumentList(node); 91 return super.visitArgumentList(node);
87 } 92 }
88 93
89 @override 94 @override
90 Object visitAsExpression(AsExpression node) { 95 Object visitAsExpression(AsExpression node) {
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 Expression expression, 302 Expression expression,
298 DartType expectedStaticType, 303 DartType expectedStaticType,
299 DartType actualStaticType, 304 DartType actualStaticType,
300 DartType expectedPropagatedType, 305 DartType expectedPropagatedType,
301 DartType actualPropagatedType, 306 DartType actualPropagatedType,
302 ErrorCode hintCode) { 307 ErrorCode hintCode) {
303 // 308 //
304 // Warning case: test static type information 309 // Warning case: test static type information
305 // 310 //
306 if (actualStaticType != null && expectedStaticType != null) { 311 if (actualStaticType != null && expectedStaticType != null) {
307 if (!actualStaticType.isAssignableTo(expectedStaticType)) { 312 if (!_typeSystem.isAssignableTo(actualStaticType, expectedStaticType)) {
308 // A warning was created in the ErrorVerifier, return false, don't 313 // A warning was created in the ErrorVerifier, return false, don't
309 // create a hint when a warning has already been created. 314 // create a hint when a warning has already been created.
310 return false; 315 return false;
311 } 316 }
312 } 317 }
313 // 318 //
314 // Hint case: test propagated type information 319 // Hint case: test propagated type information
315 // 320 //
316 // Compute the best types to use. 321 // Compute the best types to use.
317 DartType expectedBestType = expectedPropagatedType != null 322 DartType expectedBestType = expectedPropagatedType != null
318 ? expectedPropagatedType 323 ? expectedPropagatedType
319 : expectedStaticType; 324 : expectedStaticType;
320 DartType actualBestType = 325 DartType actualBestType =
321 actualPropagatedType != null ? actualPropagatedType : actualStaticType; 326 actualPropagatedType != null ? actualPropagatedType : actualStaticType;
322 if (actualBestType != null && expectedBestType != null) { 327 if (actualBestType != null && expectedBestType != null) {
323 if (!actualBestType.isAssignableTo(expectedBestType)) { 328 if (!_typeSystem.isAssignableTo(actualBestType, expectedBestType)) {
324 _errorReporter.reportTypeErrorForNode( 329 _errorReporter.reportTypeErrorForNode(
325 hintCode, expression, [actualBestType, expectedBestType]); 330 hintCode, expression, [actualBestType, expectedBestType]);
326 return true; 331 return true;
327 } 332 }
328 } 333 }
329 return false; 334 return false;
330 } 335 }
331 336
332 /** 337 /**
333 * This verifies that the passed argument can be assigned to its corresponding parameter. 338 * This verifies that the passed argument can be assigned to its corresponding parameter.
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 */ 517 */
513 bool _checkForInvalidAssignment(Expression lhs, Expression rhs) { 518 bool _checkForInvalidAssignment(Expression lhs, Expression rhs) {
514 if (lhs == null || rhs == null) { 519 if (lhs == null || rhs == null) {
515 return false; 520 return false;
516 } 521 }
517 VariableElement leftVariableElement = ErrorVerifier.getVariableElement(lhs); 522 VariableElement leftVariableElement = ErrorVerifier.getVariableElement(lhs);
518 DartType leftType = (leftVariableElement == null) 523 DartType leftType = (leftVariableElement == null)
519 ? ErrorVerifier.getStaticType(lhs) 524 ? ErrorVerifier.getStaticType(lhs)
520 : leftVariableElement.type; 525 : leftVariableElement.type;
521 DartType staticRightType = ErrorVerifier.getStaticType(rhs); 526 DartType staticRightType = ErrorVerifier.getStaticType(rhs);
522 if (!staticRightType.isAssignableTo(leftType)) { 527 if (!_typeSystem.isAssignableTo(staticRightType, leftType)) {
523 // The warning was generated on this rhs 528 // The warning was generated on this rhs
524 return false; 529 return false;
525 } 530 }
526 // Test for, and then generate the hint 531 // Test for, and then generate the hint
527 DartType bestRightType = rhs.bestType; 532 DartType bestRightType = rhs.bestType;
528 if (leftType != null && bestRightType != null) { 533 if (leftType != null && bestRightType != null) {
529 if (!bestRightType.isAssignableTo(leftType)) { 534 if (!_typeSystem.isAssignableTo(bestRightType, leftType)) {
530 _errorReporter.reportTypeErrorForNode( 535 _errorReporter.reportTypeErrorForNode(
531 HintCode.INVALID_ASSIGNMENT, rhs, [bestRightType, leftType]); 536 HintCode.INVALID_ASSIGNMENT, rhs, [bestRightType, leftType]);
532 return true; 537 return true;
533 } 538 }
534 } 539 }
535 return false; 540 return false;
536 } 541 }
537 542
538 /** 543 /**
539 * Check that the imported library does not define a loadLibrary function. The import has already 544 * Check that the imported library does not define a loadLibrary function. The import has already
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
586 if (body.isGenerator) { 591 if (body.isGenerator) {
587 return false; 592 return false;
588 } 593 }
589 // Check that the type is resolvable, and is not "void" 594 // Check that the type is resolvable, and is not "void"
590 DartType returnTypeType = returnType.type; 595 DartType returnTypeType = returnType.type;
591 if (returnTypeType == null || returnTypeType.isVoid) { 596 if (returnTypeType == null || returnTypeType.isVoid) {
592 return false; 597 return false;
593 } 598 }
594 // For async, give no hint if Future<Null> is assignable to the return 599 // For async, give no hint if Future<Null> is assignable to the return
595 // type. 600 // type.
596 if (body.isAsynchronous && _futureNullType.isAssignableTo(returnTypeType)) { 601 if (body.isAsynchronous && _typeSystem.isAssignableTo(_futureNullType, retur nTypeType)) {
597 return false; 602 return false;
598 } 603 }
599 // Check the block for a return statement, if not, create the hint 604 // Check the block for a return statement, if not, create the hint
600 BlockFunctionBody blockFunctionBody = body as BlockFunctionBody; 605 BlockFunctionBody blockFunctionBody = body as BlockFunctionBody;
601 if (!ExitDetector.exits(blockFunctionBody)) { 606 if (!ExitDetector.exits(blockFunctionBody)) {
602 _errorReporter.reportErrorForNode( 607 _errorReporter.reportErrorForNode(
603 HintCode.MISSING_RETURN, returnType, [returnTypeType.displayName]); 608 HintCode.MISSING_RETURN, returnType, [returnTypeType.displayName]);
604 return true; 609 return true;
605 } 610 }
606 return false; 611 return false;
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
832 * The error reporter by which errors will be reported. 837 * The error reporter by which errors will be reported.
833 */ 838 */
834 final ErrorReporter _errorReporter; 839 final ErrorReporter _errorReporter;
835 840
836 /** 841 /**
837 * The type provider used to access the known types. 842 * The type provider used to access the known types.
838 */ 843 */
839 final TypeProvider _typeProvider; 844 final TypeProvider _typeProvider;
840 845
841 /** 846 /**
847 * The type system in use.
848 */
849 final TypeSystem _typeSystem;
850
851 /**
842 * The set of variables declared using '-D' on the command line. 852 * The set of variables declared using '-D' on the command line.
843 */ 853 */
844 final DeclaredVariables declaredVariables; 854 final DeclaredVariables declaredVariables;
845 855
846 /** 856 /**
847 * The type representing the type 'bool'. 857 * The type representing the type 'bool'.
848 */ 858 */
849 InterfaceType _boolType; 859 InterfaceType _boolType;
850 860
851 /** 861 /**
(...skipping 15 matching lines...) Expand all
867 * The current library that is being analyzed. 877 * The current library that is being analyzed.
868 */ 878 */
869 final LibraryElement _currentLibrary; 879 final LibraryElement _currentLibrary;
870 880
871 /** 881 /**
872 * Initialize a newly created constant verifier. 882 * Initialize a newly created constant verifier.
873 * 883 *
874 * @param errorReporter the error reporter by which errors will be reported 884 * @param errorReporter the error reporter by which errors will be reported
875 */ 885 */
876 ConstantVerifier(this._errorReporter, this._currentLibrary, 886 ConstantVerifier(this._errorReporter, this._currentLibrary,
877 this._typeProvider, this.declaredVariables) { 887 this._typeProvider, this._typeSystem, this.declaredVariables) {
878 this._boolType = _typeProvider.boolType; 888 this._boolType = _typeProvider.boolType;
879 this._intType = _typeProvider.intType; 889 this._intType = _typeProvider.intType;
880 this._numType = _typeProvider.numType; 890 this._numType = _typeProvider.numType;
881 this._stringType = _typeProvider.stringType; 891 this._stringType = _typeProvider.stringType;
882 } 892 }
883 893
884 @override 894 @override
885 Object visitAnnotation(Annotation node) { 895 Object visitAnnotation(Annotation node) {
886 super.visitAnnotation(node); 896 super.visitAnnotation(node);
887 // check annotation creation 897 // check annotation creation
(...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after
1342 * @param parameterElements the elements of parameters of constant constructor , they are 1352 * @param parameterElements the elements of parameters of constant constructor , they are
1343 * considered as a valid potentially constant expressions 1353 * considered as a valid potentially constant expressions
1344 * @param expression the expression to validate 1354 * @param expression the expression to validate
1345 */ 1355 */
1346 void _validateInitializerExpression( 1356 void _validateInitializerExpression(
1347 List<ParameterElement> parameterElements, Expression expression) { 1357 List<ParameterElement> parameterElements, Expression expression) {
1348 RecordingErrorListener errorListener = new RecordingErrorListener(); 1358 RecordingErrorListener errorListener = new RecordingErrorListener();
1349 ErrorReporter subErrorReporter = 1359 ErrorReporter subErrorReporter =
1350 new ErrorReporter(errorListener, _errorReporter.source); 1360 new ErrorReporter(errorListener, _errorReporter.source);
1351 DartObjectImpl result = expression.accept( 1361 DartObjectImpl result = expression.accept(
1352 new _ConstantVerifier_validateInitializerExpression(_typeProvider, 1362 new _ConstantVerifier_validateInitializerExpression(_typeProvider, _type System,
1353 subErrorReporter, this, parameterElements, declaredVariables)); 1363 subErrorReporter, this, parameterElements, declaredVariables));
1354 _reportErrors(errorListener.errors, 1364 _reportErrors(errorListener.errors,
1355 CompileTimeErrorCode.NON_CONSTANT_VALUE_IN_INITIALIZER); 1365 CompileTimeErrorCode.NON_CONSTANT_VALUE_IN_INITIALIZER);
1356 if (result != null) { 1366 if (result != null) {
1357 _reportErrorIfFromDeferredLibrary(expression, 1367 _reportErrorIfFromDeferredLibrary(expression,
1358 CompileTimeErrorCode.NON_CONSTANT_VALUE_IN_INITIALIZER_FROM_DEFERRED_L IBRARY); 1368 CompileTimeErrorCode.NON_CONSTANT_VALUE_IN_INITIALIZER_FROM_DEFERRED_L IBRARY);
1359 } 1369 }
1360 } 1370 }
1361 1371
1362 /** 1372 /**
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
1468 * Instances of the class `DeadCodeVerifier` traverse an AST structure looking f or cases of 1478 * Instances of the class `DeadCodeVerifier` traverse an AST structure looking f or cases of
1469 * [HintCode.DEAD_CODE]. 1479 * [HintCode.DEAD_CODE].
1470 */ 1480 */
1471 class DeadCodeVerifier extends RecursiveAstVisitor<Object> { 1481 class DeadCodeVerifier extends RecursiveAstVisitor<Object> {
1472 /** 1482 /**
1473 * The error reporter by which errors will be reported. 1483 * The error reporter by which errors will be reported.
1474 */ 1484 */
1475 final ErrorReporter _errorReporter; 1485 final ErrorReporter _errorReporter;
1476 1486
1477 /** 1487 /**
1488 * The type system for this visitor
1489 */
1490 final TypeSystem _typeSystem;
1491
1492 /**
1478 * Create a new instance of the [DeadCodeVerifier]. 1493 * Create a new instance of the [DeadCodeVerifier].
1479 * 1494 *
1480 * @param errorReporter the error reporter 1495 * @param errorReporter the error reporter
1481 */ 1496 */
1482 DeadCodeVerifier(this._errorReporter); 1497 DeadCodeVerifier(this._errorReporter, this._typeSystem);
1483 1498
1484 @override 1499 @override
1485 Object visitBinaryExpression(BinaryExpression node) { 1500 Object visitBinaryExpression(BinaryExpression node) {
1486 sc.Token operator = node.operator; 1501 sc.Token operator = node.operator;
1487 bool isAmpAmp = operator.type == sc.TokenType.AMPERSAND_AMPERSAND; 1502 bool isAmpAmp = operator.type == sc.TokenType.AMPERSAND_AMPERSAND;
1488 bool isBarBar = operator.type == sc.TokenType.BAR_BAR; 1503 bool isBarBar = operator.type == sc.TokenType.BAR_BAR;
1489 if (isAmpAmp || isBarBar) { 1504 if (isAmpAmp || isBarBar) {
1490 Expression lhsCondition = node.leftOperand; 1505 Expression lhsCondition = node.leftOperand;
1491 if (!_isDebugConstant(lhsCondition)) { 1506 if (!_isDebugConstant(lhsCondition)) {
1492 EvaluationResultImpl lhsResult = _getConstantBooleanValue(lhsCondition); 1507 EvaluationResultImpl lhsResult = _getConstantBooleanValue(lhsCondition);
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
1637 CatchClause nextCatchClause = catchClauses[i + 1]; 1652 CatchClause nextCatchClause = catchClauses[i + 1];
1638 CatchClause lastCatchClause = catchClauses[numOfCatchClauses - 1]; 1653 CatchClause lastCatchClause = catchClauses[numOfCatchClauses - 1];
1639 int offset = nextCatchClause.offset; 1654 int offset = nextCatchClause.offset;
1640 int length = lastCatchClause.end - offset; 1655 int length = lastCatchClause.end - offset;
1641 _errorReporter.reportErrorForOffset( 1656 _errorReporter.reportErrorForOffset(
1642 HintCode.DEAD_CODE_CATCH_FOLLOWING_CATCH, offset, length); 1657 HintCode.DEAD_CODE_CATCH_FOLLOWING_CATCH, offset, length);
1643 return null; 1658 return null;
1644 } 1659 }
1645 } 1660 }
1646 for (DartType type in visitedTypes) { 1661 for (DartType type in visitedTypes) {
1647 if (currentType.isSubtypeOf(type)) { 1662 if (_typeSystem.isSubtypeOf(currentType, type)) {
1648 CatchClause lastCatchClause = catchClauses[numOfCatchClauses - 1]; 1663 CatchClause lastCatchClause = catchClauses[numOfCatchClauses - 1];
1649 int offset = catchClause.offset; 1664 int offset = catchClause.offset;
1650 int length = lastCatchClause.end - offset; 1665 int length = lastCatchClause.end - offset;
1651 _errorReporter.reportErrorForOffset( 1666 _errorReporter.reportErrorForOffset(
1652 HintCode.DEAD_CODE_ON_CATCH_SUBTYPE, 1667 HintCode.DEAD_CODE_ON_CATCH_SUBTYPE,
1653 offset, 1668 offset,
1654 length, 1669 length,
1655 [currentType.displayName, type.displayName]); 1670 [currentType.displayName, type.displayName]);
1656 return null; 1671 return null;
1657 } 1672 }
(...skipping 3110 matching lines...) Expand 10 before | Expand all | Expand 10 after
4768 } 4783 }
4769 _library.accept(new UnusedLocalElementsVerifier( 4784 _library.accept(new UnusedLocalElementsVerifier(
4770 _errorListener, _usedLocalElementsVisitor.usedElements)); 4785 _errorListener, _usedLocalElementsVisitor.usedElements));
4771 }); 4786 });
4772 } 4787 }
4773 4788
4774 void _generateForCompilationUnit(CompilationUnit unit, Source source) { 4789 void _generateForCompilationUnit(CompilationUnit unit, Source source) {
4775 ErrorReporter errorReporter = new ErrorReporter(_errorListener, source); 4790 ErrorReporter errorReporter = new ErrorReporter(_errorListener, source);
4776 unit.accept(_usedImportedElementsVisitor); 4791 unit.accept(_usedImportedElementsVisitor);
4777 // dead code analysis 4792 // dead code analysis
4778 unit.accept(new DeadCodeVerifier(errorReporter)); 4793 unit.accept(new DeadCodeVerifier(errorReporter, _context.typeSystem));
4779 unit.accept(_usedLocalElementsVisitor); 4794 unit.accept(_usedLocalElementsVisitor);
4780 // dart2js analysis 4795 // dart2js analysis
4781 if (_enableDart2JSHints) { 4796 if (_enableDart2JSHints) {
4782 unit.accept(new Dart2JSVerifier(errorReporter)); 4797 unit.accept(new Dart2JSVerifier(errorReporter));
4783 } 4798 }
4784 // Dart best practices 4799 // Dart best practices
4785 unit.accept( 4800 unit.accept(
4786 new BestPracticesVerifier(errorReporter, _context.typeProvider)); 4801 new BestPracticesVerifier(errorReporter, _context.typeProvider, _context .typeSystem));
4787 unit.accept(new OverrideVerifier(errorReporter, _manager)); 4802 unit.accept(new OverrideVerifier(errorReporter, _manager));
4788 // Find to-do comments 4803 // Find to-do comments
4789 new ToDoFinder(errorReporter).findIn(unit); 4804 new ToDoFinder(errorReporter).findIn(unit);
4790 // pub analysis 4805 // pub analysis
4791 // TODO(danrubel/jwren) Commented out until bugs in the pub verifier are 4806 // TODO(danrubel/jwren) Commented out until bugs in the pub verifier are
4792 // fixed 4807 // fixed
4793 // unit.accept(new PubVerifier(context, errorReporter)); 4808 // unit.accept(new PubVerifier(context, errorReporter));
4794 } 4809 }
4795 } 4810 }
4796 4811
(...skipping 1440 matching lines...) Expand 10 before | Expand all | Expand 10 after
6237 for (int i = 0; i < numOfEltsWithMatchingNames; i++) { 6252 for (int i = 0; i < numOfEltsWithMatchingNames; i++) {
6238 executableElementTypes[i] = elements[i].type; 6253 executableElementTypes[i] = elements[i].type;
6239 } 6254 }
6240 List<int> subtypesOfAllOtherTypesIndexes = new List<int>(); 6255 List<int> subtypesOfAllOtherTypesIndexes = new List<int>();
6241 for (int i = 0; i < numOfEltsWithMatchingNames; i++) { 6256 for (int i = 0; i < numOfEltsWithMatchingNames; i++) {
6242 FunctionType subtype = executableElementTypes[i]; 6257 FunctionType subtype = executableElementTypes[i];
6243 if (subtype == null) { 6258 if (subtype == null) {
6244 continue; 6259 continue;
6245 } 6260 }
6246 bool subtypeOfAllTypes = true; 6261 bool subtypeOfAllTypes = true;
6262 TypeSystem typeSystem = _library.context.typeSystem;
6247 for (int j = 0; 6263 for (int j = 0;
6248 j < numOfEltsWithMatchingNames && subtypeOfAllTypes; 6264 j < numOfEltsWithMatchingNames && subtypeOfAllTypes;
6249 j++) { 6265 j++) {
6250 if (i != j) { 6266 if (i != j) {
6251 if (!subtype.isSubtypeOf(executableElementTypes[j])) { 6267 if (!typeSystem.isSubtypeOf(subtype, executableElementTypes[j])) {
6252 subtypeOfAllTypes = false; 6268 subtypeOfAllTypes = false;
6253 break; 6269 break;
6254 } 6270 }
6255 } 6271 }
6256 } 6272 }
6257 if (subtypeOfAllTypes) { 6273 if (subtypeOfAllTypes) {
6258 subtypesOfAllOtherTypesIndexes.add(i); 6274 subtypesOfAllOtherTypesIndexes.add(i);
6259 } 6275 }
6260 } 6276 }
6261 // 6277 //
(...skipping 1277 matching lines...) Expand 10 before | Expand all | Expand 10 after
7539 * The object representing the async library. 7555 * The object representing the async library.
7540 */ 7556 */
7541 Library _asyncLibrary; 7557 Library _asyncLibrary;
7542 7558
7543 /** 7559 /**
7544 * The object used to access the types from the core library. 7560 * The object used to access the types from the core library.
7545 */ 7561 */
7546 TypeProvider _typeProvider; 7562 TypeProvider _typeProvider;
7547 7563
7548 /** 7564 /**
7565 * The type system in use for the library
7566 */
7567 TypeSystem _typeSystem;
7568
7569 /**
7549 * A table mapping library sources to the information being maintained for tho se libraries. 7570 * A table mapping library sources to the information being maintained for tho se libraries.
7550 */ 7571 */
7551 HashMap<Source, Library> _libraryMap = new HashMap<Source, Library>(); 7572 HashMap<Source, Library> _libraryMap = new HashMap<Source, Library>();
7552 7573
7553 /** 7574 /**
7554 * A collection containing the libraries that are being resolved together. 7575 * A collection containing the libraries that are being resolved together.
7555 */ 7576 */
7556 Set<Library> _librariesInCycles; 7577 Set<Library> _librariesInCycles;
7557 7578
7558 /** 7579 /**
(...skipping 22 matching lines...) Expand all
7581 * @return an array containing the libraries that were resolved 7602 * @return an array containing the libraries that were resolved
7582 */ 7603 */
7583 Set<Library> get resolvedLibraries => _librariesInCycles; 7604 Set<Library> get resolvedLibraries => _librariesInCycles;
7584 7605
7585 /** 7606 /**
7586 * The object used to access the types from the core library. 7607 * The object used to access the types from the core library.
7587 */ 7608 */
7588 TypeProvider get typeProvider => _typeProvider; 7609 TypeProvider get typeProvider => _typeProvider;
7589 7610
7590 /** 7611 /**
7612 * The type system in use.
7613 */
7614 TypeSystem get typeSystem => _typeSystem;
7615
7616 /**
7591 * Create an object to represent the information about the library defined by the compilation unit 7617 * Create an object to represent the information about the library defined by the compilation unit
7592 * with the given source. 7618 * with the given source.
7593 * 7619 *
7594 * @param librarySource the source of the library's defining compilation unit 7620 * @param librarySource the source of the library's defining compilation unit
7595 * @return the library object that was created 7621 * @return the library object that was created
7596 * @throws AnalysisException if the library source is not valid 7622 * @throws AnalysisException if the library source is not valid
7597 */ 7623 */
7598 Library createLibrary(Source librarySource) { 7624 Library createLibrary(Source librarySource) {
7599 Library library = 7625 Library library =
7600 new Library(analysisContext, _errorListener, librarySource); 7626 new Library(analysisContext, _errorListener, librarySource);
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
7665 LibraryElement coreElement = _coreLibrary.libraryElement; 7691 LibraryElement coreElement = _coreLibrary.libraryElement;
7666 if (coreElement == null) { 7692 if (coreElement == null) {
7667 throw new AnalysisException("Could not resolve dart:core"); 7693 throw new AnalysisException("Could not resolve dart:core");
7668 } 7694 }
7669 LibraryElement asyncElement = _asyncLibrary.libraryElement; 7695 LibraryElement asyncElement = _asyncLibrary.libraryElement;
7670 if (asyncElement == null) { 7696 if (asyncElement == null) {
7671 throw new AnalysisException("Could not resolve dart:async"); 7697 throw new AnalysisException("Could not resolve dart:async");
7672 } 7698 }
7673 _buildDirectiveModels(); 7699 _buildDirectiveModels();
7674 _typeProvider = new TypeProviderImpl(coreElement, asyncElement); 7700 _typeProvider = new TypeProviderImpl(coreElement, asyncElement);
7701 _typeSystem = new TypeSystemImpl(typeProvider);
7675 _buildTypeHierarchies(); 7702 _buildTypeHierarchies();
7676 // 7703 //
7677 // Perform resolution and type analysis. 7704 // Perform resolution and type analysis.
7678 // 7705 //
7679 // TODO(brianwilkerson) Decide whether we want to resolve all of the 7706 // TODO(brianwilkerson) Decide whether we want to resolve all of the
7680 // libraries or whether we want to only resolve the target library. 7707 // libraries or whether we want to only resolve the target library.
7681 // The advantage to resolving everything is that we have already done part 7708 // The advantage to resolving everything is that we have already done part
7682 // of the work so we'll avoid duplicated effort. The disadvantage of 7709 // of the work so we'll avoid duplicated effort. The disadvantage of
7683 // resolving everything is that we might do extra work that we don't 7710 // resolving everything is that we might do extra work that we don't
7684 // really care about. Another possibility is to add a parameter to this 7711 // really care about. Another possibility is to add a parameter to this
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
7743 LibraryElement coreElement = _coreLibrary.libraryElement; 7770 LibraryElement coreElement = _coreLibrary.libraryElement;
7744 if (coreElement == null) { 7771 if (coreElement == null) {
7745 throw new AnalysisException("Could not resolve dart:core"); 7772 throw new AnalysisException("Could not resolve dart:core");
7746 } 7773 }
7747 LibraryElement asyncElement = _asyncLibrary.libraryElement; 7774 LibraryElement asyncElement = _asyncLibrary.libraryElement;
7748 if (asyncElement == null) { 7775 if (asyncElement == null) {
7749 throw new AnalysisException("Could not resolve dart:async"); 7776 throw new AnalysisException("Could not resolve dart:async");
7750 } 7777 }
7751 _buildDirectiveModels(); 7778 _buildDirectiveModels();
7752 _typeProvider = new TypeProviderImpl(coreElement, asyncElement); 7779 _typeProvider = new TypeProviderImpl(coreElement, asyncElement);
7780 _typeSystem = new TypeSystemImpl(typeProvider);
7753 _buildEnumMembers(); 7781 _buildEnumMembers();
7754 _buildTypeHierarchies(); 7782 _buildTypeHierarchies();
7755 // 7783 //
7756 // Perform resolution and type analysis. 7784 // Perform resolution and type analysis.
7757 // 7785 //
7758 // TODO(brianwilkerson) Decide whether we want to resolve all of the 7786 // TODO(brianwilkerson) Decide whether we want to resolve all of the
7759 // libraries or whether we want to only resolve the target library. The 7787 // libraries or whether we want to only resolve the target library. The
7760 // advantage to resolving everything is that we have already done part of 7788 // advantage to resolving everything is that we have already done part of
7761 // the work so we'll avoid duplicated effort. The disadvantage of 7789 // the work so we'll avoid duplicated effort. The disadvantage of
7762 // resolving everything is that we might do extra work that we don't 7790 // resolving everything is that we might do extra work that we don't
(...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after
8283 for (Library library in _librariesInCycles) { 8311 for (Library library in _librariesInCycles) {
8284 for (Source source in library.compilationUnitSources) { 8312 for (Source source in library.compilationUnitSources) {
8285 try { 8313 try {
8286 CompilationUnit unit = library.getAST(source); 8314 CompilationUnit unit = library.getAST(source);
8287 ErrorReporter errorReporter = 8315 ErrorReporter errorReporter =
8288 new ErrorReporter(_errorListener, source); 8316 new ErrorReporter(_errorListener, source);
8289 ConstantVerifier constantVerifier = new ConstantVerifier( 8317 ConstantVerifier constantVerifier = new ConstantVerifier(
8290 errorReporter, 8318 errorReporter,
8291 library.libraryElement, 8319 library.libraryElement,
8292 _typeProvider, 8320 _typeProvider,
8321 _typeSystem,
8293 analysisContext.declaredVariables); 8322 analysisContext.declaredVariables);
8294 unit.accept(constantVerifier); 8323 unit.accept(constantVerifier);
8295 } on AnalysisException catch (exception, stackTrace) { 8324 } on AnalysisException catch (exception, stackTrace) {
8296 AnalysisEngine.instance.logger.logError( 8325 AnalysisEngine.instance.logger.logError(
8297 "Internal Error: Could not access AST for ${source.fullName} " 8326 "Internal Error: Could not access AST for ${source.fullName} "
8298 "during constant verification", 8327 "during constant verification",
8299 new CaughtException(exception, stackTrace)); 8328 new CaughtException(exception, stackTrace));
8300 } 8329 }
8301 } 8330 }
8302 } 8331 }
(...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after
8773 for (ResolvableLibrary library in _librariesInCycle) { 8802 for (ResolvableLibrary library in _librariesInCycle) {
8774 for (ResolvableCompilationUnit unit 8803 for (ResolvableCompilationUnit unit
8775 in library.resolvableCompilationUnits) { 8804 in library.resolvableCompilationUnits) {
8776 CompilationUnit ast = unit.compilationUnit; 8805 CompilationUnit ast = unit.compilationUnit;
8777 ErrorReporter errorReporter = 8806 ErrorReporter errorReporter =
8778 new ErrorReporter(_errorListener, unit.source); 8807 new ErrorReporter(_errorListener, unit.source);
8779 ConstantVerifier constantVerifier = new ConstantVerifier( 8808 ConstantVerifier constantVerifier = new ConstantVerifier(
8780 errorReporter, 8809 errorReporter,
8781 library.libraryElement, 8810 library.libraryElement,
8782 _typeProvider, 8811 _typeProvider,
8812 analysisContext.typeSystem,
8783 analysisContext.declaredVariables); 8813 analysisContext.declaredVariables);
8784 ast.accept(constantVerifier); 8814 ast.accept(constantVerifier);
8785 } 8815 }
8786 } 8816 }
8787 }); 8817 });
8788 } 8818 }
8789 8819
8790 /** 8820 /**
8791 * Resolve the identifiers and perform type analysis in the libraries in the c urrent cycle. 8821 * Resolve the identifiers and perform type analysis in the libraries in the c urrent cycle.
8792 * 8822 *
(...skipping 5930 matching lines...) Expand 10 before | Expand all | Expand 10 after
14723 * Return the [TypeProvider] associated with this [TypeSystem]. 14753 * Return the [TypeProvider] associated with this [TypeSystem].
14724 */ 14754 */
14725 TypeProvider get typeProvider; 14755 TypeProvider get typeProvider;
14726 14756
14727 /** 14757 /**
14728 * Compute the least upper bound of two types. 14758 * Compute the least upper bound of two types.
14729 */ 14759 */
14730 DartType getLeastUpperBound(DartType type1, DartType type2); 14760 DartType getLeastUpperBound(DartType type1, DartType type2);
14731 14761
14732 /** 14762 /**
14763 * Return `true` if the [leftType] is assignable to the [rightType] (that is,
14764 * if leftType <==> rightType).
14765 */
14766 bool isAssignableTo(DartType leftType, DartType rightType);
14767
14768 /**
14733 * Return `true` if the [leftType] is a subtype of the [rightType] (that is, 14769 * Return `true` if the [leftType] is a subtype of the [rightType] (that is,
14734 * if leftType <: rightType). 14770 * if leftType <: rightType).
14735 */ 14771 */
14736 bool isSubtypeOf(DartType leftType, DartType rightType); 14772 bool isSubtypeOf(DartType leftType, DartType rightType);
14737 } 14773 }
14738 14774
14739 /** 14775 /**
14740 * Implementation of [TypeSystem] using the rules in the Dart specification. 14776 * Implementation of [TypeSystem] using the rules in the Dart specification.
14741 */ 14777 */
14742 class TypeSystemImpl implements TypeSystem { 14778 class TypeSystemImpl implements TypeSystem {
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
14818 } 14854 }
14819 return result; 14855 return result;
14820 } else { 14856 } else {
14821 // Should never happen. As a defensive measure, return the dynamic type. 14857 // Should never happen. As a defensive measure, return the dynamic type.
14822 assert(false); 14858 assert(false);
14823 return typeProvider.dynamicType; 14859 return typeProvider.dynamicType;
14824 } 14860 }
14825 } 14861 }
14826 14862
14827 @override 14863 @override
14864 bool isAssignableTo(DartType leftType, DartType rightType) {
14865 return leftType.isAssignableTo(rightType);
14866 }
14867
14868 @override
14828 bool isSubtypeOf(DartType leftType, DartType rightType) { 14869 bool isSubtypeOf(DartType leftType, DartType rightType) {
14829 return leftType.isSubtypeOf(rightType); 14870 return leftType.isSubtypeOf(rightType);
14830 } 14871 }
14831 } 14872 }
14832 14873
14833 /** 14874 /**
14834 * Instances of the class [UnusedLocalElementsVerifier] traverse an element 14875 * Instances of the class [UnusedLocalElementsVerifier] traverse an element
14835 * structure looking for cases of [HintCode.UNUSED_ELEMENT], 14876 * structure looking for cases of [HintCode.UNUSED_ELEMENT],
14836 * [HintCode.UNUSED_FIELD], [HintCode.UNUSED_LOCAL_VARIABLE], etc. 14877 * [HintCode.UNUSED_FIELD], [HintCode.UNUSED_LOCAL_VARIABLE], etc.
14837 */ 14878 */
(...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after
15225 } 15266 }
15226 return null; 15267 return null;
15227 } 15268 }
15228 } 15269 }
15229 15270
15230 class _ConstantVerifier_validateInitializerExpression extends ConstantVisitor { 15271 class _ConstantVerifier_validateInitializerExpression extends ConstantVisitor {
15231 final ConstantVerifier verifier; 15272 final ConstantVerifier verifier;
15232 15273
15233 List<ParameterElement> parameterElements; 15274 List<ParameterElement> parameterElements;
15234 15275
15276 TypeSystem _typeSystem;
15277
15235 _ConstantVerifier_validateInitializerExpression( 15278 _ConstantVerifier_validateInitializerExpression(
15236 TypeProvider typeProvider, 15279 TypeProvider typeProvider,
15280 this._typeSystem,
15237 ErrorReporter errorReporter, 15281 ErrorReporter errorReporter,
15238 this.verifier, 15282 this.verifier,
15239 this.parameterElements, 15283 this.parameterElements,
15240 DeclaredVariables declaredVariables) 15284 DeclaredVariables declaredVariables)
15241 : super(new ConstantEvaluationEngine(typeProvider, declaredVariables), 15285 : super(new ConstantEvaluationEngine(typeProvider, declaredVariables),
15242 errorReporter); 15286 errorReporter);
15243 15287
15244 @override 15288 @override
15245 DartObjectImpl visitSimpleIdentifier(SimpleIdentifier node) { 15289 DartObjectImpl visitSimpleIdentifier(SimpleIdentifier node) {
15246 Element element = node.staticElement; 15290 Element element = node.staticElement;
15247 for (ParameterElement parameterElement in parameterElements) { 15291 for (ParameterElement parameterElement in parameterElements) {
15248 if (identical(parameterElement, element) && parameterElement != null) { 15292 if (identical(parameterElement, element) && parameterElement != null) {
15249 DartType type = parameterElement.type; 15293 DartType type = parameterElement.type;
15250 if (type != null) { 15294 if (type != null) {
15251 if (type.isDynamic) { 15295 if (type.isDynamic) {
15252 return new DartObjectImpl( 15296 return new DartObjectImpl(
15253 verifier._typeProvider.objectType, DynamicState.DYNAMIC_STATE); 15297 verifier._typeProvider.objectType, DynamicState.DYNAMIC_STATE);
15254 } else if (type.isSubtypeOf(verifier._boolType)) { 15298 } else if (_typeSystem.isSubtypeOf(type, verifier._boolType)) {
15255 return new DartObjectImpl( 15299 return new DartObjectImpl(
15256 verifier._typeProvider.boolType, BoolState.UNKNOWN_VALUE); 15300 verifier._typeProvider.boolType, BoolState.UNKNOWN_VALUE);
15257 } else if (type.isSubtypeOf(verifier._typeProvider.doubleType)) { 15301 } else if (_typeSystem.isSubtypeOf(type, verifier._typeProvider.double Type)) {
15258 return new DartObjectImpl( 15302 return new DartObjectImpl(
15259 verifier._typeProvider.doubleType, DoubleState.UNKNOWN_VALUE); 15303 verifier._typeProvider.doubleType, DoubleState.UNKNOWN_VALUE);
15260 } else if (type.isSubtypeOf(verifier._intType)) { 15304 } else if (_typeSystem.isSubtypeOf(type, verifier._intType)) {
15261 return new DartObjectImpl( 15305 return new DartObjectImpl(
15262 verifier._typeProvider.intType, IntState.UNKNOWN_VALUE); 15306 verifier._typeProvider.intType, IntState.UNKNOWN_VALUE);
15263 } else if (type.isSubtypeOf(verifier._numType)) { 15307 } else if (_typeSystem.isSubtypeOf(type, verifier._numType)) {
15264 return new DartObjectImpl( 15308 return new DartObjectImpl(
15265 verifier._typeProvider.numType, NumState.UNKNOWN_VALUE); 15309 verifier._typeProvider.numType, NumState.UNKNOWN_VALUE);
15266 } else if (type.isSubtypeOf(verifier._stringType)) { 15310 } else if (_typeSystem.isSubtypeOf(type, verifier._stringType)) {
15267 return new DartObjectImpl( 15311 return new DartObjectImpl(
15268 verifier._typeProvider.stringType, StringState.UNKNOWN_VALUE); 15312 verifier._typeProvider.stringType, StringState.UNKNOWN_VALUE);
15269 } 15313 }
15270 // 15314 //
15271 // We don't test for other types of objects (such as List, Map, 15315 // We don't test for other types of objects (such as List, Map,
15272 // Function or Type) because there are no operations allowed on such 15316 // Function or Type) because there are no operations allowed on such
15273 // types other than '==' and '!=', which means that we don't need to 15317 // types other than '==' and '!=', which means that we don't need to
15274 // know the type when there is no specific data about the state of 15318 // know the type when there is no specific data about the state of
15275 // such objects. 15319 // such objects.
15276 // 15320 //
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
15389 nonFields.add(node); 15433 nonFields.add(node);
15390 return null; 15434 return null;
15391 } 15435 }
15392 15436
15393 @override 15437 @override
15394 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this); 15438 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this);
15395 15439
15396 @override 15440 @override
15397 Object visitWithClause(WithClause node) => null; 15441 Object visitWithClause(WithClause node) => null;
15398 } 15442 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698