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 library engine.resolver; | 5 library engine.resolver; |
6 | 6 |
7 import "dart:math" as math; | 7 import "dart:math" as math; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 | 9 |
10 import 'package:analyzer/src/generated/utilities_collection.dart'; | 10 import 'package:analyzer/src/generated/utilities_collection.dart'; |
(...skipping 10951 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
10962 // Join overrides. | 10962 // Join overrides. |
10963 bool thenIsAbrupt = _isAbruptTerminationStatement(thenStatement); | 10963 bool thenIsAbrupt = _isAbruptTerminationStatement(thenStatement); |
10964 bool elseIsAbrupt = _isAbruptTerminationStatement(elseStatement); | 10964 bool elseIsAbrupt = _isAbruptTerminationStatement(elseStatement); |
10965 if (elseIsAbrupt && !thenIsAbrupt) { | 10965 if (elseIsAbrupt && !thenIsAbrupt) { |
10966 _propagateTrueState(condition); | 10966 _propagateTrueState(condition); |
10967 _overrideManager.applyOverrides(thenOverrides); | 10967 _overrideManager.applyOverrides(thenOverrides); |
10968 } else if (thenIsAbrupt && !elseIsAbrupt) { | 10968 } else if (thenIsAbrupt && !elseIsAbrupt) { |
10969 _propagateFalseState(condition); | 10969 _propagateFalseState(condition); |
10970 _overrideManager.applyOverrides(elseOverrides); | 10970 _overrideManager.applyOverrides(elseOverrides); |
10971 } else if (!thenIsAbrupt && !elseIsAbrupt) { | 10971 } else if (!thenIsAbrupt && !elseIsAbrupt) { |
| 10972 List<Map<VariableElement, DartType>> perBranchOverrides = |
| 10973 new List<Map<VariableElement, DartType>>(); |
| 10974 perBranchOverrides.add(thenOverrides); |
| 10975 perBranchOverrides.add(elseOverrides); |
10972 if (AnalysisEngine.instance.enableUnionTypes) { | 10976 if (AnalysisEngine.instance.enableUnionTypes) { |
10973 List<Map<VariableElement, DartType>> perBranchOverrides = | |
10974 new List<Map<VariableElement, DartType>>(); | |
10975 perBranchOverrides.add(thenOverrides); | |
10976 perBranchOverrides.add(elseOverrides); | |
10977 _overrideManager.joinOverrides(perBranchOverrides); | 10977 _overrideManager.joinOverrides(perBranchOverrides); |
| 10978 } else { |
| 10979 _overrideManager.mergeOverrides(perBranchOverrides); |
10978 } | 10980 } |
10979 } | 10981 } |
10980 return null; | 10982 return null; |
10981 } | 10983 } |
10982 | 10984 |
10983 @override | 10985 @override |
10984 Object visitLabel(Label node) => null; | 10986 Object visitLabel(Label node) => null; |
10985 | 10987 |
10986 @override | 10988 @override |
10987 Object visitLibraryIdentifier(LibraryIdentifier node) => null; | 10989 Object visitLibraryIdentifier(LibraryIdentifier node) => null; |
(...skipping 1781 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
12769 for (Map<VariableElement, DartType> os in perBranchOverrides) { | 12771 for (Map<VariableElement, DartType> os in perBranchOverrides) { |
12770 if (os.containsKey(e)) { | 12772 if (os.containsKey(e)) { |
12771 joinOverrides[e] = UnionTypeImpl.union([joinOverrides[e], os[e]]); | 12773 joinOverrides[e] = UnionTypeImpl.union([joinOverrides[e], os[e]]); |
12772 } | 12774 } |
12773 } | 12775 } |
12774 } | 12776 } |
12775 applyOverrides(joinOverrides); | 12777 applyOverrides(joinOverrides); |
12776 } | 12778 } |
12777 | 12779 |
12778 /** | 12780 /** |
| 12781 * Update overrides assuming [perBranchOverrides] is the collection of |
| 12782 * per-branch overrides for *all* branches flowing into a join point. |
| 12783 * |
| 12784 * If a variable type in any of branches is not the same as its type before |
| 12785 * the branching, then its propagated type is reset to `null`. |
| 12786 */ |
| 12787 void mergeOverrides(List<Map<VariableElement, DartType>> perBranchOverrides) { |
| 12788 for (Map<VariableElement, DartType> branch in perBranchOverrides) { |
| 12789 branch.forEach((VariableElement variable, DartType branchType) { |
| 12790 DartType currentType = _currentScope.getType(variable); |
| 12791 if (currentType != branchType) { |
| 12792 _currentScope.resetType(variable); |
| 12793 } |
| 12794 }); |
| 12795 } |
| 12796 } |
| 12797 |
| 12798 /** |
12779 * Set the overridden type of the given element to the given type | 12799 * Set the overridden type of the given element to the given type |
12780 * | 12800 * |
12781 * @param element the element whose type might have been overridden | 12801 * @param element the element whose type might have been overridden |
12782 * @param type the overridden type of the given element | 12802 * @param type the overridden type of the given element |
12783 */ | 12803 */ |
12784 void setType(VariableElement element, DartType type) { | 12804 void setType(VariableElement element, DartType type) { |
12785 if (_currentScope == null) { | 12805 if (_currentScope == null) { |
12786 throw new IllegalStateException("Cannot override without a scope"); | 12806 throw new IllegalStateException("Cannot override without a scope"); |
12787 } | 12807 } |
12788 _currentScope.setType(element, type); | 12808 _currentScope.setType(element, type); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
12855 } | 12875 } |
12856 | 12876 |
12857 /** | 12877 /** |
12858 * Return the overridden type of the given element, or `null` if the type of t
he element | 12878 * Return the overridden type of the given element, or `null` if the type of t
he element |
12859 * has not been overridden. | 12879 * has not been overridden. |
12860 * | 12880 * |
12861 * @param element the element whose type might have been overridden | 12881 * @param element the element whose type might have been overridden |
12862 * @return the overridden type of the given element | 12882 * @return the overridden type of the given element |
12863 */ | 12883 */ |
12864 DartType getType(Element element) { | 12884 DartType getType(Element element) { |
| 12885 if (element is PropertyAccessorElement) { |
| 12886 element = (element as PropertyAccessorElement).variable; |
| 12887 } |
12865 DartType type = _overridenTypes[element]; | 12888 DartType type = _overridenTypes[element]; |
12866 if (type == null && element is PropertyAccessorElement) { | 12889 if (_overridenTypes.containsKey(element)) { |
12867 type = _overridenTypes[element.variable]; | 12890 return type; |
12868 } | 12891 } |
12869 if (type != null) { | 12892 if (type != null) { |
12870 return type; | 12893 return type; |
12871 } else if (_outerScope != null) { | 12894 } else if (_outerScope != null) { |
12872 return _outerScope.getType(element); | 12895 return _outerScope.getType(element); |
12873 } | 12896 } |
12874 return null; | 12897 return null; |
12875 } | 12898 } |
12876 | 12899 |
12877 /** | 12900 /** |
| 12901 * Clears the overridden type of the given [element]. |
| 12902 */ |
| 12903 void resetType(VariableElement element) { |
| 12904 _overridenTypes[element] = null; |
| 12905 } |
| 12906 |
| 12907 /** |
12878 * Set the overridden type of the given element to the given type | 12908 * Set the overridden type of the given element to the given type |
12879 * | 12909 * |
12880 * @param element the element whose type might have been overridden | 12910 * @param element the element whose type might have been overridden |
12881 * @param type the overridden type of the given element | 12911 * @param type the overridden type of the given element |
12882 */ | 12912 */ |
12883 void setType(VariableElement element, DartType type) { | 12913 void setType(VariableElement element, DartType type) { |
12884 _overridenTypes[element] = type; | 12914 _overridenTypes[element] = type; |
12885 } | 12915 } |
12886 } | 12916 } |
12887 | 12917 |
(...skipping 2487 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
15375 } | 15405 } |
15376 | 15406 |
15377 bool isCatchException(LocalVariableElement element) { | 15407 bool isCatchException(LocalVariableElement element) { |
15378 return catchExceptionElements.contains(element); | 15408 return catchExceptionElements.contains(element); |
15379 } | 15409 } |
15380 | 15410 |
15381 bool isCatchStackTrace(LocalVariableElement element) { | 15411 bool isCatchStackTrace(LocalVariableElement element) { |
15382 return catchStackTraceElements.contains(element); | 15412 return catchStackTraceElements.contains(element); |
15383 } | 15413 } |
15384 } | 15414 } |
OLD | NEW |