| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 interface TreeElements { | 5 interface TreeElements { |
| 6 Element operator[](Node node); | 6 Element operator[](Node node); |
| 7 Selector getSelector(Send send); | 7 Selector getSelector(Send send); |
| 8 DartType getType(TypeAnnotation annotation); | 8 DartType getType(TypeAnnotation annotation); |
| 9 bool isParameterChecked(Element element); | 9 bool isParameterChecked(Element element); |
| 10 } | 10 } |
| (...skipping 934 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 945 } | 945 } |
| 946 return arguments.toLink(); | 946 return arguments.toLink(); |
| 947 } | 947 } |
| 948 } | 948 } |
| 949 | 949 |
| 950 class ResolverVisitor extends CommonResolverVisitor<Element> { | 950 class ResolverVisitor extends CommonResolverVisitor<Element> { |
| 951 final TreeElementMapping mapping; | 951 final TreeElementMapping mapping; |
| 952 final Element enclosingElement; | 952 final Element enclosingElement; |
| 953 final TypeResolver typeResolver; | 953 final TypeResolver typeResolver; |
| 954 bool inInstanceContext; | 954 bool inInstanceContext; |
| 955 bool inCheckContext; | |
| 956 Scope scope; | 955 Scope scope; |
| 957 ClassElement currentClass; | 956 ClassElement currentClass; |
| 958 bool typeRequired = false; | 957 bool typeRequired = false; |
| 959 StatementScope statementScope; | 958 StatementScope statementScope; |
| 960 int allowedCategory = ElementCategory.VARIABLE | ElementCategory.FUNCTION; | 959 int allowedCategory = ElementCategory.VARIABLE | ElementCategory.FUNCTION; |
| 961 | 960 |
| 962 ResolverVisitor(Compiler compiler, Element element) | 961 ResolverVisitor(Compiler compiler, Element element) |
| 963 : this.mapping = new TreeElementMapping(), | 962 : this.mapping = new TreeElementMapping(), |
| 964 this.enclosingElement = element, | 963 this.enclosingElement = element, |
| 965 // When the element is a field, we are actually resolving its | 964 // When the element is a field, we are actually resolving its |
| 966 // initial value, which should not have access to instance | 965 // initial value, which should not have access to instance |
| 967 // fields. | 966 // fields. |
| 968 inInstanceContext = (element.isInstanceMember() && !element.isField()) | 967 inInstanceContext = (element.isInstanceMember() && !element.isField()) |
| 969 || element.isGenerativeConstructor(), | 968 || element.isGenerativeConstructor(), |
| 970 this.currentClass = element.isMember() ? element.getEnclosingClass() | 969 this.currentClass = element.isMember() ? |
| 971 : null, | 970 element.getEnclosingClass() : |
| 971 null, |
| 972 this.statementScope = new StatementScope(), | 972 this.statementScope = new StatementScope(), |
| 973 typeResolver = new TypeResolver(compiler), | 973 typeResolver = new TypeResolver(compiler), |
| 974 scope = element.buildEnclosingScope(), | 974 scope = element.buildEnclosingScope(), |
| 975 inCheckContext = compiler.enableTypeAssertions, | |
| 976 super(compiler) { | 975 super(compiler) { |
| 977 } | 976 } |
| 978 | 977 |
| 979 Enqueuer get world => compiler.enqueuer.resolution; | 978 Enqueuer get world => compiler.enqueuer.resolution; |
| 980 | 979 |
| 981 Element lookup(Node node, SourceString name) { | 980 Element lookup(Node node, SourceString name) { |
| 982 Element result = scope.lookup(name); | 981 Element result = scope.lookup(name); |
| 983 if (!inInstanceContext && result != null && result.isInstanceMember()) { | 982 if (!inInstanceContext && result != null && result.isInstanceMember()) { |
| 984 error(node, MessageKind.NO_INSTANCE_AVAILABLE, [node]); | 983 error(node, MessageKind.NO_INSTANCE_AVAILABLE, [node]); |
| 985 } | 984 } |
| 986 return result; | 985 return result; |
| 987 } | 986 } |
| 988 | 987 |
| 989 // Create, or reuse an already created, statement element for a statement. | 988 // Create, or reuse an already created, statement element for a statement. |
| 990 TargetElement getOrCreateTargetElement(Node statement) { | 989 TargetElement getOrCreateTargetElement(Node statement) { |
| 991 TargetElement element = mapping[statement]; | 990 TargetElement element = mapping[statement]; |
| 992 if (element === null) { | 991 if (element === null) { |
| 993 element = new TargetElement(statement, | 992 element = new TargetElement(statement, |
| 994 statementScope.nestingLevel, | 993 statementScope.nestingLevel, |
| 995 enclosingElement); | 994 enclosingElement); |
| 996 mapping[statement] = element; | 995 mapping[statement] = element; |
| 997 } | 996 } |
| 998 return element; | 997 return element; |
| 999 } | 998 } |
| 1000 | 999 |
| 1001 doInCheckContext(action()) { | |
| 1002 bool wasInCheckContext = inCheckContext; | |
| 1003 inCheckContext = true; | |
| 1004 var result = action(); | |
| 1005 inCheckContext = wasInCheckContext; | |
| 1006 return result; | |
| 1007 } | |
| 1008 | |
| 1009 inStaticContext(action()) { | 1000 inStaticContext(action()) { |
| 1010 bool wasInstanceContext = inInstanceContext; | 1001 bool wasInstanceContext = inInstanceContext; |
| 1011 inInstanceContext = false; | 1002 inInstanceContext = false; |
| 1012 var result = action(); | 1003 var result = action(); |
| 1013 inInstanceContext = wasInstanceContext; | 1004 inInstanceContext = wasInstanceContext; |
| 1014 return result; | 1005 return result; |
| 1015 } | 1006 } |
| 1016 | 1007 |
| 1017 visitInStaticContext(Node node) { | 1008 visitInStaticContext(Node node) { |
| 1018 inStaticContext(() => visit(node)); | 1009 inStaticContext(() => visit(node)); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 1039 // TODO(ahe): Improve error message. Need UX input. | 1030 // TODO(ahe): Improve error message. Need UX input. |
| 1040 error(node, MessageKind.GENERIC, ["is not an expression $element"]); | 1031 error(node, MessageKind.GENERIC, ["is not an expression $element"]); |
| 1041 } | 1032 } |
| 1042 } | 1033 } |
| 1043 return useElement(node, element); | 1034 return useElement(node, element); |
| 1044 } | 1035 } |
| 1045 } | 1036 } |
| 1046 | 1037 |
| 1047 Element visitTypeAnnotation(TypeAnnotation node) { | 1038 Element visitTypeAnnotation(TypeAnnotation node) { |
| 1048 DartType type = resolveTypeAnnotation(node); | 1039 DartType type = resolveTypeAnnotation(node); |
| 1049 if (type !== null) { | 1040 if (type !== null) return type.element; |
| 1050 if (inCheckContext) { | |
| 1051 compiler.enqueuer.resolution.registerIsCheck(type); | |
| 1052 } | |
| 1053 return type.element; | |
| 1054 } | |
| 1055 return null; | 1041 return null; |
| 1056 } | 1042 } |
| 1057 | 1043 |
| 1058 Element defineElement(Node node, Element element, | 1044 Element defineElement(Node node, Element element, |
| 1059 [bool doAddToScope = true]) { | 1045 [bool doAddToScope = true]) { |
| 1060 compiler.ensure(element !== null); | 1046 compiler.ensure(element !== null); |
| 1061 mapping[node] = element; | 1047 mapping[node] = element; |
| 1062 if (doAddToScope) { | 1048 if (doAddToScope) { |
| 1063 Element existing = scope.add(element); | 1049 Element existing = scope.add(element); |
| 1064 if (existing != element) { | 1050 if (existing != element) { |
| (...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1354 // TODO(karlklose): make this a runtime error. | 1340 // TODO(karlklose): make this a runtime error. |
| 1355 error(node.selector, MessageKind.CANNOT_RESOLVE_GETTER); | 1341 error(node.selector, MessageKind.CANNOT_RESOLVE_GETTER); |
| 1356 } | 1342 } |
| 1357 } | 1343 } |
| 1358 | 1344 |
| 1359 bool resolvedArguments = false; | 1345 bool resolvedArguments = false; |
| 1360 if (node.isOperator) { | 1346 if (node.isOperator) { |
| 1361 String operatorString = node.selector.asOperator().source.stringValue; | 1347 String operatorString = node.selector.asOperator().source.stringValue; |
| 1362 if (operatorString === 'is' || operatorString === 'as') { | 1348 if (operatorString === 'is' || operatorString === 'as') { |
| 1363 assert(node.arguments.tail.isEmpty()); | 1349 assert(node.arguments.tail.isEmpty()); |
| 1364 DartType type = resolveTypeTest(node.arguments.head); | 1350 resolveTypeTest(node.arguments.head); |
| 1365 if (type != null) { | |
| 1366 compiler.enqueuer.resolution.registerIsCheck(type); | |
| 1367 } | |
| 1368 resolvedArguments = true; | 1351 resolvedArguments = true; |
| 1369 } else if (operatorString === '?') { | 1352 } else if (operatorString === '?') { |
| 1370 Element parameter = mapping[node.receiver]; | 1353 Element parameter = mapping[node.receiver]; |
| 1371 if (parameter === null || parameter.kind !== ElementKind.PARAMETER) { | 1354 if (parameter === null || parameter.kind !== ElementKind.PARAMETER) { |
| 1372 error(node.receiver, MessageKind.PARAMETER_NAME_EXPECTED); | 1355 error(node.receiver, MessageKind.PARAMETER_NAME_EXPECTED); |
| 1373 } else { | 1356 } else { |
| 1374 mapping.checkedParameters.add(parameter); | 1357 mapping.checkedParameters.add(parameter); |
| 1375 } | 1358 } |
| 1376 } | 1359 } |
| 1377 } | 1360 } |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1618 DartType resolveTypeRequired(TypeAnnotation node) { | 1601 DartType resolveTypeRequired(TypeAnnotation node) { |
| 1619 bool old = typeRequired; | 1602 bool old = typeRequired; |
| 1620 typeRequired = true; | 1603 typeRequired = true; |
| 1621 DartType result = resolveTypeAnnotation(node); | 1604 DartType result = resolveTypeAnnotation(node); |
| 1622 typeRequired = old; | 1605 typeRequired = old; |
| 1623 return result; | 1606 return result; |
| 1624 } | 1607 } |
| 1625 | 1608 |
| 1626 DartType resolveTypeAnnotation(TypeAnnotation node) { | 1609 DartType resolveTypeAnnotation(TypeAnnotation node) { |
| 1627 Function report = typeRequired ? error : warning; | 1610 Function report = typeRequired ? error : warning; |
| 1628 DartType type = typeResolver.resolveTypeAnnotation(node, inScope: scope, | 1611 return typeResolver.resolveTypeAnnotation(node, inScope: scope, |
| 1629 onFailure: report, | 1612 onFailure: report, |
| 1630 whenResolved: useType); | 1613 whenResolved: useType); |
| 1631 if (inCheckContext && type != null) { | |
| 1632 compiler.enqueuer.resolution.registerIsCheck(type); | |
| 1633 } | |
| 1634 return type; | |
| 1635 } | 1614 } |
| 1636 | 1615 |
| 1637 visitModifiers(Modifiers node) { | 1616 visitModifiers(Modifiers node) { |
| 1638 // TODO(ngeoffray): Implement this. | 1617 // TODO(ngeoffray): Implement this. |
| 1639 unimplemented(node, 'modifiers'); | 1618 unimplemented(node, 'modifiers'); |
| 1640 } | 1619 } |
| 1641 | 1620 |
| 1642 visitLiteralList(LiteralList node) { | 1621 visitLiteralList(LiteralList node) { |
| 1643 NodeList arguments = node.typeArguments; | 1622 NodeList arguments = node.typeArguments; |
| 1644 if (arguments !== null) { | 1623 if (arguments !== null) { |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1899 error(modifier, MessageKind.PARAMETER_WITH_MODIFIER_IN_CATCH); | 1878 error(modifier, MessageKind.PARAMETER_WITH_MODIFIER_IN_CATCH); |
| 1900 } | 1879 } |
| 1901 TypeAnnotation type = declaration.type; | 1880 TypeAnnotation type = declaration.type; |
| 1902 if (type !== null) { | 1881 if (type !== null) { |
| 1903 error(type, MessageKind.PARAMETER_WITH_TYPE_IN_CATCH); | 1882 error(type, MessageKind.PARAMETER_WITH_TYPE_IN_CATCH); |
| 1904 } | 1883 } |
| 1905 } | 1884 } |
| 1906 } | 1885 } |
| 1907 | 1886 |
| 1908 Scope blockScope = new BlockScope(scope); | 1887 Scope blockScope = new BlockScope(scope); |
| 1909 doInCheckContext(() => visitIn(node.type, blockScope)); | 1888 visitIn(node.type, blockScope); |
| 1910 visitIn(node.formals, blockScope); | 1889 visitIn(node.formals, blockScope); |
| 1911 visitIn(node.block, blockScope); | 1890 visitIn(node.block, blockScope); |
| 1912 } | 1891 } |
| 1913 | 1892 |
| 1914 visitTypedef(Typedef node) { | 1893 visitTypedef(Typedef node) { |
| 1915 unimplemented(node, 'typedef'); | 1894 unimplemented(node, 'typedef'); |
| 1916 } | 1895 } |
| 1917 } | 1896 } |
| 1918 | 1897 |
| 1919 class TypeDefinitionVisitor extends CommonResolverVisitor<DartType> { | 1898 class TypeDefinitionVisitor extends CommonResolverVisitor<DartType> { |
| (...skipping 767 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2687 TopScope(LibraryElement library) : super(null, library); | 2666 TopScope(LibraryElement library) : super(null, library); |
| 2688 Element lookup(SourceString name) { | 2667 Element lookup(SourceString name) { |
| 2689 return library.find(name); | 2668 return library.find(name); |
| 2690 } | 2669 } |
| 2691 | 2670 |
| 2692 Element add(Element newElement) { | 2671 Element add(Element newElement) { |
| 2693 throw "Cannot add an element in the top scope"; | 2672 throw "Cannot add an element in the top scope"; |
| 2694 } | 2673 } |
| 2695 String toString() => '$element'; | 2674 String toString() => '$element'; |
| 2696 } | 2675 } |
| OLD | NEW |