| 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 analyzer.src.generated.element_resolver; | 5 library analyzer.src.generated.element_resolver; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:analyzer/dart/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
| 10 import 'package:analyzer/dart/ast/token.dart'; | 10 import 'package:analyzer/dart/ast/token.dart'; |
| (...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 ClassElement enclosingClass = _resolver.enclosingClass; | 344 ClassElement enclosingClass = _resolver.enclosingClass; |
| 345 FieldElement fieldElement = enclosingClass.getField(fieldName.name); | 345 FieldElement fieldElement = enclosingClass.getField(fieldName.name); |
| 346 fieldName.staticElement = fieldElement; | 346 fieldName.staticElement = fieldElement; |
| 347 return null; | 347 return null; |
| 348 } | 348 } |
| 349 | 349 |
| 350 @override | 350 @override |
| 351 Object visitConstructorName(ConstructorName node) { | 351 Object visitConstructorName(ConstructorName node) { |
| 352 DartType type = node.type.type; | 352 DartType type = node.type.type; |
| 353 if (type != null && type.isDynamic) { | 353 if (type != null && type.isDynamic) { |
| 354 return null; | 354 // Nothing to do. |
| 355 } else if (type is! InterfaceType) { | 355 } else if (type is InterfaceType) { |
| 356 // look up ConstructorElement |
| 357 ConstructorElement constructor; |
| 358 SimpleIdentifier name = node.name; |
| 359 if (name == null) { |
| 360 constructor = type.lookUpConstructor(null, _definingLibrary); |
| 361 } else { |
| 362 constructor = type.lookUpConstructor(name.name, _definingLibrary); |
| 363 name.staticElement = constructor; |
| 364 } |
| 365 node.staticElement = constructor; |
| 366 } else { |
| 356 // TODO(brianwilkerson) Report these errors. | 367 // TODO(brianwilkerson) Report these errors. |
| 357 // ASTNode parent = node.getParent(); | 368 // ASTNode parent = node.getParent(); |
| 358 // if (parent instanceof InstanceCreationExpression) { | 369 // if (parent instanceof InstanceCreationExpression) { |
| 359 // if (((InstanceCreationExpression) parent).isConst()) { | 370 // if (((InstanceCreationExpression) parent).isConst()) { |
| 360 // // CompileTimeErrorCode.CONST_WITH_NON_TYPE | 371 // // CompileTimeErrorCode.CONST_WITH_NON_TYPE |
| 361 // } else { | 372 // } else { |
| 362 // // StaticWarningCode.NEW_WITH_NON_TYPE | 373 // // StaticWarningCode.NEW_WITH_NON_TYPE |
| 363 // } | 374 // } |
| 364 // } else { | 375 // } else { |
| 365 // // This is part of a redirecting factory constructor; not sure which e
rror code to use | 376 // // This is part of a redirecting factory constructor; not sure which e
rror code to use |
| 366 // } | 377 // } |
| 367 return null; | |
| 368 } | 378 } |
| 369 // look up ConstructorElement | |
| 370 ConstructorElement constructor; | |
| 371 SimpleIdentifier name = node.name; | |
| 372 InterfaceType interfaceType = type as InterfaceType; | |
| 373 if (name == null) { | |
| 374 constructor = interfaceType.lookUpConstructor(null, _definingLibrary); | |
| 375 } else { | |
| 376 constructor = | |
| 377 interfaceType.lookUpConstructor(name.name, _definingLibrary); | |
| 378 name.staticElement = constructor; | |
| 379 } | |
| 380 node.staticElement = constructor; | |
| 381 return null; | 379 return null; |
| 382 } | 380 } |
| 383 | 381 |
| 384 @override | 382 @override |
| 385 Object visitContinueStatement(ContinueStatement node) { | 383 Object visitContinueStatement(ContinueStatement node) { |
| 386 node.target = _lookupBreakOrContinueTarget(node, node.label, true); | 384 node.target = _lookupBreakOrContinueTarget(node, node.label, true); |
| 387 return null; | 385 return null; |
| 388 } | 386 } |
| 389 | 387 |
| 390 @override | 388 @override |
| (...skipping 687 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1078 _resolver.reportErrorForNode( | 1076 _resolver.reportErrorForNode( |
| 1079 CompileTimeErrorCode.INVALID_CONSTRUCTOR_NAME, node); | 1077 CompileTimeErrorCode.INVALID_CONSTRUCTOR_NAME, node); |
| 1080 element = null; | 1078 element = null; |
| 1081 } else if (element == null || | 1079 } else if (element == null || |
| 1082 (element is PrefixElement && !_isValidAsPrefix(node))) { | 1080 (element is PrefixElement && !_isValidAsPrefix(node))) { |
| 1083 // TODO(brianwilkerson) Recover from this error. | 1081 // TODO(brianwilkerson) Recover from this error. |
| 1084 if (_isConstructorReturnType(node)) { | 1082 if (_isConstructorReturnType(node)) { |
| 1085 _resolver.reportErrorForNode( | 1083 _resolver.reportErrorForNode( |
| 1086 CompileTimeErrorCode.INVALID_CONSTRUCTOR_NAME, node); | 1084 CompileTimeErrorCode.INVALID_CONSTRUCTOR_NAME, node); |
| 1087 } else { | 1085 } else { |
| 1088 AstNode parent = node.parent; | |
| 1089 if (parent is Annotation) { | 1086 if (parent is Annotation) { |
| 1090 _resolver.reportErrorForNode( | 1087 _resolver.reportErrorForNode( |
| 1091 CompileTimeErrorCode.INVALID_ANNOTATION, parent); | 1088 CompileTimeErrorCode.INVALID_ANNOTATION, parent); |
| 1092 } else if (element != null) { | 1089 } else if (element != null) { |
| 1093 _resolver.reportErrorForNode( | 1090 _resolver.reportErrorForNode( |
| 1094 CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, | 1091 CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, |
| 1095 node, | 1092 node, |
| 1096 [element.name]); | 1093 [element.name]); |
| 1097 } else { | 1094 } else { |
| 1098 _recordUndefinedNode(_resolver.enclosingClass, | 1095 _recordUndefinedNode(_resolver.enclosingClass, |
| 1099 StaticWarningCode.UNDEFINED_IDENTIFIER, node, [node.name]); | 1096 StaticWarningCode.UNDEFINED_IDENTIFIER, node, [node.name]); |
| 1100 } | 1097 } |
| 1101 } | 1098 } |
| 1102 } | 1099 } |
| 1103 node.staticElement = element; | 1100 node.staticElement = element; |
| 1104 if (node.inSetterContext() && | 1101 if (node.inSetterContext() && |
| 1105 node.inGetterContext() && | 1102 node.inGetterContext() && |
| 1106 enclosingClass != null) { | 1103 enclosingClass != null) { |
| 1107 InterfaceType enclosingType = enclosingClass.type; | 1104 InterfaceType enclosingType = enclosingClass.type; |
| 1108 AuxiliaryElements auxiliaryElements = new AuxiliaryElements( | 1105 AuxiliaryElements auxiliaryElements = new AuxiliaryElements( |
| 1109 _lookUpGetter(null, enclosingType, node.name), null); | 1106 _lookUpGetter(null, enclosingType, node.name), null); |
| 1110 node.auxiliaryElements = auxiliaryElements; | 1107 node.auxiliaryElements = auxiliaryElements; |
| 1111 } | 1108 } |
| 1112 // | 1109 // |
| 1113 // Validate annotation element. | 1110 // Validate annotation element. |
| 1114 // | 1111 // |
| 1115 if (node.parent is Annotation) { | 1112 if (parent is Annotation) { |
| 1116 Annotation annotation = node.parent as Annotation; | 1113 _resolveAnnotationElement(parent); |
| 1117 _resolveAnnotationElement(annotation); | |
| 1118 } | 1114 } |
| 1119 return null; | 1115 return null; |
| 1120 } | 1116 } |
| 1121 | 1117 |
| 1122 @override | 1118 @override |
| 1123 Object visitSuperConstructorInvocation(SuperConstructorInvocation node) { | 1119 Object visitSuperConstructorInvocation(SuperConstructorInvocation node) { |
| 1124 ClassElementImpl enclosingClass = | 1120 ClassElementImpl enclosingClass = |
| 1125 ClassElementImpl.getImpl(_resolver.enclosingClass); | 1121 ClassElementImpl.getImpl(_resolver.enclosingClass); |
| 1126 if (enclosingClass == null) { | 1122 if (enclosingClass == null) { |
| 1127 // TODO(brianwilkerson) Report this error. | 1123 // TODO(brianwilkerson) Report this error. |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1410 // | 1406 // |
| 1411 // All function types are subtypes of 'Function', which is itself a | 1407 // All function types are subtypes of 'Function', which is itself a |
| 1412 // subclass of 'Object'. | 1408 // subclass of 'Object'. |
| 1413 // | 1409 // |
| 1414 bestType = _resolver.typeProvider.functionType; | 1410 bestType = _resolver.typeProvider.functionType; |
| 1415 } | 1411 } |
| 1416 return bestType; | 1412 return bestType; |
| 1417 } | 1413 } |
| 1418 | 1414 |
| 1419 /** | 1415 /** |
| 1420 * Assuming that the given [expression] is a prefix for a deferred import, | 1416 * Assuming that the given [identifier] is a prefix for a deferred import, |
| 1421 * return the library that is being imported. | 1417 * return the library that is being imported. |
| 1422 */ | 1418 */ |
| 1423 LibraryElement _getImportedLibrary(Expression expression) { | 1419 LibraryElement _getImportedLibrary(SimpleIdentifier identifier) { |
| 1424 PrefixElement prefixElement = | 1420 PrefixElement prefixElement = identifier.staticElement as PrefixElement; |
| 1425 (expression as SimpleIdentifier).staticElement as PrefixElement; | |
| 1426 List<ImportElement> imports = | 1421 List<ImportElement> imports = |
| 1427 prefixElement.enclosingElement.getImportsWithPrefix(prefixElement); | 1422 prefixElement.enclosingElement.getImportsWithPrefix(prefixElement); |
| 1428 return imports[0].importedLibrary; | 1423 return imports[0].importedLibrary; |
| 1429 } | 1424 } |
| 1430 | 1425 |
| 1431 /** | 1426 /** |
| 1432 * Given an element, computes the type of the invocation. | 1427 * Given an element, computes the type of the invocation. |
| 1433 * | 1428 * |
| 1434 * For executable elements (like methods, functions) this is just their type. | 1429 * For executable elements (like methods, functions) this is just their type. |
| 1435 * | 1430 * |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1537 } | 1532 } |
| 1538 } | 1533 } |
| 1539 } | 1534 } |
| 1540 return invokeType; | 1535 return invokeType; |
| 1541 } | 1536 } |
| 1542 | 1537 |
| 1543 /** | 1538 /** |
| 1544 * Return `true` if the given [expression] is a prefix for a deferred import. | 1539 * Return `true` if the given [expression] is a prefix for a deferred import. |
| 1545 */ | 1540 */ |
| 1546 bool _isDeferredPrefix(Expression expression) { | 1541 bool _isDeferredPrefix(Expression expression) { |
| 1547 if (expression is! SimpleIdentifier) { | 1542 if (expression is SimpleIdentifier) { |
| 1548 return false; | 1543 Element element = expression.staticElement; |
| 1544 if (element is PrefixElement) { |
| 1545 List<ImportElement> imports = |
| 1546 element.enclosingElement.getImportsWithPrefix(element); |
| 1547 if (imports.length != 1) { |
| 1548 return false; |
| 1549 } |
| 1550 return imports[0].isDeferred; |
| 1551 } |
| 1549 } | 1552 } |
| 1550 Element element = (expression as SimpleIdentifier).staticElement; | 1553 return false; |
| 1551 if (element is! PrefixElement) { | |
| 1552 return false; | |
| 1553 } | |
| 1554 PrefixElement prefixElement = element as PrefixElement; | |
| 1555 List<ImportElement> imports = | |
| 1556 prefixElement.enclosingElement.getImportsWithPrefix(prefixElement); | |
| 1557 if (imports.length != 1) { | |
| 1558 return false; | |
| 1559 } | |
| 1560 return imports[0].isDeferred; | |
| 1561 } | 1554 } |
| 1562 | 1555 |
| 1563 /** | 1556 /** |
| 1564 * Return `true` if the given [type] represents an object that could be | 1557 * Return `true` if the given [type] represents an object that could be |
| 1565 * invoked using the call operator '()'. | 1558 * invoked using the call operator '()'. |
| 1566 */ | 1559 */ |
| 1567 bool _isExecutableType(DartType type) { | 1560 bool _isExecutableType(DartType type) { |
| 1568 if (type.isDynamic || type is FunctionType) { | 1561 if (type.isDynamic || type is FunctionType) { |
| 1569 return true; | 1562 return true; |
| 1570 } else if (!_enableStrictCallChecks && | 1563 } else if (!_enableStrictCallChecks && |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1863 | 1856 |
| 1864 /** | 1857 /** |
| 1865 * Continues resolution of the given [annotation]. | 1858 * Continues resolution of the given [annotation]. |
| 1866 */ | 1859 */ |
| 1867 void _resolveAnnotationElement(Annotation annotation) { | 1860 void _resolveAnnotationElement(Annotation annotation) { |
| 1868 SimpleIdentifier nameNode1; | 1861 SimpleIdentifier nameNode1; |
| 1869 SimpleIdentifier nameNode2; | 1862 SimpleIdentifier nameNode2; |
| 1870 { | 1863 { |
| 1871 Identifier annName = annotation.name; | 1864 Identifier annName = annotation.name; |
| 1872 if (annName is PrefixedIdentifier) { | 1865 if (annName is PrefixedIdentifier) { |
| 1873 PrefixedIdentifier prefixed = annName; | 1866 nameNode1 = annName.prefix; |
| 1874 nameNode1 = prefixed.prefix; | 1867 nameNode2 = annName.identifier; |
| 1875 nameNode2 = prefixed.identifier; | |
| 1876 } else { | 1868 } else { |
| 1877 nameNode1 = annName as SimpleIdentifier; | 1869 nameNode1 = annName as SimpleIdentifier; |
| 1878 nameNode2 = null; | 1870 nameNode2 = null; |
| 1879 } | 1871 } |
| 1880 } | 1872 } |
| 1881 SimpleIdentifier nameNode3 = annotation.constructorName; | 1873 SimpleIdentifier nameNode3 = annotation.constructorName; |
| 1882 ConstructorElement constructor = null; | 1874 ConstructorElement constructor = null; |
| 1883 // | 1875 // |
| 1884 // CONST or Class(args) | 1876 // CONST or Class(args) |
| 1885 // | 1877 // |
| (...skipping 690 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2576 | 2568 |
| 2577 @override | 2569 @override |
| 2578 Element get staticElement => null; | 2570 Element get staticElement => null; |
| 2579 | 2571 |
| 2580 @override | 2572 @override |
| 2581 dynamic/*=E*/ accept/*<E>*/(AstVisitor/*<E>*/ visitor) => null; | 2573 dynamic/*=E*/ accept/*<E>*/(AstVisitor/*<E>*/ visitor) => null; |
| 2582 | 2574 |
| 2583 @override | 2575 @override |
| 2584 void visitChildren(AstVisitor visitor) {} | 2576 void visitChildren(AstVisitor visitor) {} |
| 2585 } | 2577 } |
| OLD | NEW |