| 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/visitor.dart'; | 10 import 'package:analyzer/dart/ast/visitor.dart'; |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 } | 198 } |
| 199 | 199 |
| 200 @override | 200 @override |
| 201 Object visitBreakStatement(BreakStatement node) { | 201 Object visitBreakStatement(BreakStatement node) { |
| 202 node.target = _lookupBreakOrContinueTarget(node, node.label, false); | 202 node.target = _lookupBreakOrContinueTarget(node, node.label, false); |
| 203 return null; | 203 return null; |
| 204 } | 204 } |
| 205 | 205 |
| 206 @override | 206 @override |
| 207 Object visitClassDeclaration(ClassDeclaration node) { | 207 Object visitClassDeclaration(ClassDeclaration node) { |
| 208 resolveMetadata(node); | 208 setMetadata(node.element, node); |
| 209 return null; | 209 return null; |
| 210 } | 210 } |
| 211 | 211 |
| 212 @override | 212 @override |
| 213 Object visitClassTypeAlias(ClassTypeAlias node) { | 213 Object visitClassTypeAlias(ClassTypeAlias node) { |
| 214 resolveMetadata(node); | 214 setMetadata(node.element, node); |
| 215 return null; | 215 return null; |
| 216 } | 216 } |
| 217 | 217 |
| 218 @override | 218 @override |
| 219 Object visitCommentReference(CommentReference node) { | 219 Object visitCommentReference(CommentReference node) { |
| 220 Identifier identifier = node.identifier; | 220 Identifier identifier = node.identifier; |
| 221 if (identifier is SimpleIdentifier) { | 221 if (identifier is SimpleIdentifier) { |
| 222 SimpleIdentifier simpleIdentifier = identifier; | 222 SimpleIdentifier simpleIdentifier = identifier; |
| 223 Element element = _resolveSimpleIdentifier(simpleIdentifier); | 223 Element element = _resolveSimpleIdentifier(simpleIdentifier); |
| 224 if (element == null) { | 224 if (element == null) { |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 constructorElement.redirectedConstructor = redirectedElement; | 329 constructorElement.redirectedConstructor = redirectedElement; |
| 330 } else { | 330 } else { |
| 331 // set redirected generative constructor | 331 // set redirected generative constructor |
| 332 for (ConstructorInitializer initializer in node.initializers) { | 332 for (ConstructorInitializer initializer in node.initializers) { |
| 333 if (initializer is RedirectingConstructorInvocation) { | 333 if (initializer is RedirectingConstructorInvocation) { |
| 334 ConstructorElement redirectedElement = initializer.staticElement; | 334 ConstructorElement redirectedElement = initializer.staticElement; |
| 335 constructorElement.redirectedConstructor = redirectedElement; | 335 constructorElement.redirectedConstructor = redirectedElement; |
| 336 } | 336 } |
| 337 } | 337 } |
| 338 } | 338 } |
| 339 resolveMetadata(node); | 339 setMetadata(constructorElement, node); |
| 340 } | 340 } |
| 341 return null; | 341 return null; |
| 342 } | 342 } |
| 343 | 343 |
| 344 @override | 344 @override |
| 345 Object visitConstructorFieldInitializer(ConstructorFieldInitializer node) { | 345 Object visitConstructorFieldInitializer(ConstructorFieldInitializer node) { |
| 346 SimpleIdentifier fieldName = node.fieldName; | 346 SimpleIdentifier fieldName = node.fieldName; |
| 347 ClassElement enclosingClass = _resolver.enclosingClass; | 347 ClassElement enclosingClass = _resolver.enclosingClass; |
| 348 FieldElement fieldElement = enclosingClass.getField(fieldName.name); | 348 FieldElement fieldElement = enclosingClass.getField(fieldName.name); |
| 349 fieldName.staticElement = fieldElement; | 349 fieldName.staticElement = fieldElement; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 } | 385 } |
| 386 | 386 |
| 387 @override | 387 @override |
| 388 Object visitContinueStatement(ContinueStatement node) { | 388 Object visitContinueStatement(ContinueStatement node) { |
| 389 node.target = _lookupBreakOrContinueTarget(node, node.label, true); | 389 node.target = _lookupBreakOrContinueTarget(node, node.label, true); |
| 390 return null; | 390 return null; |
| 391 } | 391 } |
| 392 | 392 |
| 393 @override | 393 @override |
| 394 Object visitDeclaredIdentifier(DeclaredIdentifier node) { | 394 Object visitDeclaredIdentifier(DeclaredIdentifier node) { |
| 395 resolveMetadata(node); | 395 setMetadata(node.element, node); |
| 396 return null; | 396 return null; |
| 397 } | 397 } |
| 398 | 398 |
| 399 @override | 399 @override |
| 400 Object visitEnumDeclaration(EnumDeclaration node) { | 400 Object visitEnumDeclaration(EnumDeclaration node) { |
| 401 resolveMetadata(node); | 401 setMetadata(node.element, node); |
| 402 return null; | 402 return null; |
| 403 } | 403 } |
| 404 | 404 |
| 405 @override | 405 @override |
| 406 Object visitExportDirective(ExportDirective node) { | 406 Object visitExportDirective(ExportDirective node) { |
| 407 ExportElement exportElement = node.element; | 407 ExportElement exportElement = node.element; |
| 408 if (exportElement != null) { | 408 if (exportElement != null) { |
| 409 // The element is null when the URI is invalid | 409 // The element is null when the URI is invalid |
| 410 // TODO(brianwilkerson) Figure out whether the element can ever be | 410 // TODO(brianwilkerson) Figure out whether the element can ever be |
| 411 // something other than an ExportElement | 411 // something other than an ExportElement |
| 412 _resolveCombinators(exportElement.exportedLibrary, node.combinators); | 412 _resolveCombinators(exportElement.exportedLibrary, node.combinators); |
| 413 resolveMetadata(node); | 413 setMetadata(exportElement, node); |
| 414 } | 414 } |
| 415 return null; | 415 return null; |
| 416 } | 416 } |
| 417 | 417 |
| 418 @override | 418 @override |
| 419 Object visitFieldFormalParameter(FieldFormalParameter node) { | 419 Object visitFieldFormalParameter(FieldFormalParameter node) { |
| 420 _resolveMetadataForParameter(node.element, node); | 420 _setMetadataForParameter(node.element, node); |
| 421 return super.visitFieldFormalParameter(node); | 421 return super.visitFieldFormalParameter(node); |
| 422 } | 422 } |
| 423 | 423 |
| 424 @override | 424 @override |
| 425 Object visitFunctionDeclaration(FunctionDeclaration node) { | 425 Object visitFunctionDeclaration(FunctionDeclaration node) { |
| 426 resolveMetadata(node); | 426 setMetadata(node.element, node); |
| 427 return null; | 427 return null; |
| 428 } | 428 } |
| 429 | 429 |
| 430 @override | 430 @override |
| 431 Object visitFunctionExpressionInvocation(FunctionExpressionInvocation node) { | 431 Object visitFunctionExpressionInvocation(FunctionExpressionInvocation node) { |
| 432 Expression function = node.function; | 432 Expression function = node.function; |
| 433 DartType staticInvokeType = _instantiateGenericMethod( | 433 DartType staticInvokeType = _instantiateGenericMethod( |
| 434 function.staticType, node.typeArguments, node); | 434 function.staticType, node.typeArguments, node); |
| 435 DartType propagatedInvokeType = _instantiateGenericMethod( | 435 DartType propagatedInvokeType = _instantiateGenericMethod( |
| 436 function.propagatedType, node.typeArguments, node); | 436 function.propagatedType, node.typeArguments, node); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 448 parameters = _computeCorrespondingParameters( | 448 parameters = _computeCorrespondingParameters( |
| 449 node.argumentList, propagatedInvokeType); | 449 node.argumentList, propagatedInvokeType); |
| 450 if (parameters != null) { | 450 if (parameters != null) { |
| 451 node.argumentList.correspondingPropagatedParameters = parameters; | 451 node.argumentList.correspondingPropagatedParameters = parameters; |
| 452 } | 452 } |
| 453 return null; | 453 return null; |
| 454 } | 454 } |
| 455 | 455 |
| 456 @override | 456 @override |
| 457 Object visitFunctionTypeAlias(FunctionTypeAlias node) { | 457 Object visitFunctionTypeAlias(FunctionTypeAlias node) { |
| 458 resolveMetadata(node); | 458 setMetadata(node.element, node); |
| 459 return null; | 459 return null; |
| 460 } | 460 } |
| 461 | 461 |
| 462 @override | 462 @override |
| 463 Object visitFunctionTypedFormalParameter(FunctionTypedFormalParameter node) { | 463 Object visitFunctionTypedFormalParameter(FunctionTypedFormalParameter node) { |
| 464 _resolveMetadataForParameter(node.element, node); | 464 _setMetadataForParameter(node.element, node); |
| 465 return null; | 465 return null; |
| 466 } | 466 } |
| 467 | 467 |
| 468 @override | 468 @override |
| 469 Object visitImportDirective(ImportDirective node) { | 469 Object visitImportDirective(ImportDirective node) { |
| 470 SimpleIdentifier prefixNode = node.prefix; | 470 SimpleIdentifier prefixNode = node.prefix; |
| 471 if (prefixNode != null) { | 471 if (prefixNode != null) { |
| 472 String prefixName = prefixNode.name; | 472 String prefixName = prefixNode.name; |
| 473 for (PrefixElement prefixElement in _definingLibrary.prefixes) { | 473 for (PrefixElement prefixElement in _definingLibrary.prefixes) { |
| 474 if (prefixElement.displayName == prefixName) { | 474 if (prefixElement.displayName == prefixName) { |
| 475 prefixNode.staticElement = prefixElement; | 475 prefixNode.staticElement = prefixElement; |
| 476 break; | 476 break; |
| 477 } | 477 } |
| 478 } | 478 } |
| 479 } | 479 } |
| 480 ImportElement importElement = node.element; | 480 ImportElement importElement = node.element; |
| 481 if (importElement != null) { | 481 if (importElement != null) { |
| 482 // The element is null when the URI is invalid | 482 // The element is null when the URI is invalid |
| 483 LibraryElement library = importElement.importedLibrary; | 483 LibraryElement library = importElement.importedLibrary; |
| 484 if (library != null) { | 484 if (library != null) { |
| 485 _resolveCombinators(library, node.combinators); | 485 _resolveCombinators(library, node.combinators); |
| 486 } | 486 } |
| 487 resolveMetadata(node); | 487 setMetadata(importElement, node); |
| 488 } | 488 } |
| 489 return null; | 489 return null; |
| 490 } | 490 } |
| 491 | 491 |
| 492 @override | 492 @override |
| 493 Object visitIndexExpression(IndexExpression node) { | 493 Object visitIndexExpression(IndexExpression node) { |
| 494 Expression target = node.realTarget; | 494 Expression target = node.realTarget; |
| 495 DartType staticType = _getStaticType(target); | 495 DartType staticType = _getStaticType(target); |
| 496 DartType propagatedType = _getPropagatedType(target); | 496 DartType propagatedType = _getPropagatedType(target); |
| 497 String getterMethodName = sc.TokenType.INDEX.lexeme; | 497 String getterMethodName = sc.TokenType.INDEX.lexeme; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 570 List<ParameterElement> parameters = _resolveArgumentsToFunction( | 570 List<ParameterElement> parameters = _resolveArgumentsToFunction( |
| 571 node.isConst, argumentList, invokedConstructor); | 571 node.isConst, argumentList, invokedConstructor); |
| 572 if (parameters != null) { | 572 if (parameters != null) { |
| 573 argumentList.correspondingStaticParameters = parameters; | 573 argumentList.correspondingStaticParameters = parameters; |
| 574 } | 574 } |
| 575 return null; | 575 return null; |
| 576 } | 576 } |
| 577 | 577 |
| 578 @override | 578 @override |
| 579 Object visitLibraryDirective(LibraryDirective node) { | 579 Object visitLibraryDirective(LibraryDirective node) { |
| 580 resolveMetadata(node); | 580 setMetadata(node.element, node); |
| 581 return null; | 581 return null; |
| 582 } | 582 } |
| 583 | 583 |
| 584 @override | 584 @override |
| 585 Object visitMethodDeclaration(MethodDeclaration node) { | 585 Object visitMethodDeclaration(MethodDeclaration node) { |
| 586 resolveMetadata(node); | 586 setMetadata(node.element, node); |
| 587 return null; | 587 return null; |
| 588 } | 588 } |
| 589 | 589 |
| 590 @override | 590 @override |
| 591 Object visitMethodInvocation(MethodInvocation node) { | 591 Object visitMethodInvocation(MethodInvocation node) { |
| 592 SimpleIdentifier methodName = node.methodName; | 592 SimpleIdentifier methodName = node.methodName; |
| 593 // | 593 // |
| 594 // Synthetic identifiers have been already reported during parsing. | 594 // Synthetic identifiers have been already reported during parsing. |
| 595 // | 595 // |
| 596 if (methodName.isSynthetic) { | 596 if (methodName.isSynthetic) { |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 814 } | 814 } |
| 815 String targetTypeName = targetType == null ? null : targetType.name; | 815 String targetTypeName = targetType == null ? null : targetType.name; |
| 816 _resolver.reportErrorForNode(StaticTypeWarningCode.UNDEFINED_SUPER_METHOD, | 816 _resolver.reportErrorForNode(StaticTypeWarningCode.UNDEFINED_SUPER_METHOD, |
| 817 methodName, [methodName.name, targetTypeName]); | 817 methodName, [methodName.name, targetTypeName]); |
| 818 } | 818 } |
| 819 return null; | 819 return null; |
| 820 } | 820 } |
| 821 | 821 |
| 822 @override | 822 @override |
| 823 Object visitPartDirective(PartDirective node) { | 823 Object visitPartDirective(PartDirective node) { |
| 824 resolveMetadata(node); | 824 setMetadata(node.element, node); |
| 825 return null; | 825 return null; |
| 826 } | 826 } |
| 827 | 827 |
| 828 @override | 828 @override |
| 829 Object visitPartOfDirective(PartOfDirective node) { | 829 Object visitPartOfDirective(PartOfDirective node) { |
| 830 resolveMetadata(node); | 830 setMetadata(node.element, node); |
| 831 return null; | 831 return null; |
| 832 } | 832 } |
| 833 | 833 |
| 834 @override | 834 @override |
| 835 Object visitPostfixExpression(PostfixExpression node) { | 835 Object visitPostfixExpression(PostfixExpression node) { |
| 836 Expression operand = node.operand; | 836 Expression operand = node.operand; |
| 837 String methodName = _getPostfixOperator(node); | 837 String methodName = _getPostfixOperator(node); |
| 838 DartType staticType = _getStaticType(operand); | 838 DartType staticType = _getStaticType(operand); |
| 839 MethodElement staticMethod = _lookUpMethod(operand, staticType, methodName); | 839 MethodElement staticMethod = _lookUpMethod(operand, staticType, methodName); |
| 840 node.staticElement = staticMethod; | 840 node.staticElement = staticMethod; |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1022 List<ParameterElement> parameters = | 1022 List<ParameterElement> parameters = |
| 1023 _resolveArgumentsToFunction(false, argumentList, element); | 1023 _resolveArgumentsToFunction(false, argumentList, element); |
| 1024 if (parameters != null) { | 1024 if (parameters != null) { |
| 1025 argumentList.correspondingStaticParameters = parameters; | 1025 argumentList.correspondingStaticParameters = parameters; |
| 1026 } | 1026 } |
| 1027 return null; | 1027 return null; |
| 1028 } | 1028 } |
| 1029 | 1029 |
| 1030 @override | 1030 @override |
| 1031 Object visitSimpleFormalParameter(SimpleFormalParameter node) { | 1031 Object visitSimpleFormalParameter(SimpleFormalParameter node) { |
| 1032 _resolveMetadataForParameter(node.element, node); | 1032 _setMetadataForParameter(node.element, node); |
| 1033 return null; | 1033 return null; |
| 1034 } | 1034 } |
| 1035 | 1035 |
| 1036 @override | 1036 @override |
| 1037 Object visitSimpleIdentifier(SimpleIdentifier node) { | 1037 Object visitSimpleIdentifier(SimpleIdentifier node) { |
| 1038 // | 1038 // |
| 1039 // Synthetic identifiers have been already reported during parsing. | 1039 // Synthetic identifiers have been already reported during parsing. |
| 1040 // | 1040 // |
| 1041 if (node.isSynthetic) { | 1041 if (node.isSynthetic) { |
| 1042 return null; | 1042 return null; |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1176 Object visitSuperExpression(SuperExpression node) { | 1176 Object visitSuperExpression(SuperExpression node) { |
| 1177 if (!_isSuperInValidContext(node)) { | 1177 if (!_isSuperInValidContext(node)) { |
| 1178 _resolver.reportErrorForNode( | 1178 _resolver.reportErrorForNode( |
| 1179 CompileTimeErrorCode.SUPER_IN_INVALID_CONTEXT, node); | 1179 CompileTimeErrorCode.SUPER_IN_INVALID_CONTEXT, node); |
| 1180 } | 1180 } |
| 1181 return super.visitSuperExpression(node); | 1181 return super.visitSuperExpression(node); |
| 1182 } | 1182 } |
| 1183 | 1183 |
| 1184 @override | 1184 @override |
| 1185 Object visitTypeParameter(TypeParameter node) { | 1185 Object visitTypeParameter(TypeParameter node) { |
| 1186 resolveMetadata(node); | 1186 setMetadata(node.element, node); |
| 1187 return null; | 1187 return null; |
| 1188 } | 1188 } |
| 1189 | 1189 |
| 1190 @override | 1190 @override |
| 1191 Object visitVariableDeclaration(VariableDeclaration node) { | 1191 Object visitVariableDeclaration(VariableDeclaration node) { |
| 1192 resolveMetadata(node); | 1192 setMetadata(node.element, node); |
| 1193 return null; | 1193 return null; |
| 1194 } | 1194 } |
| 1195 | 1195 |
| 1196 /** | 1196 /** |
| 1197 * Given that we have found code to invoke the given [element], return the | 1197 * Given that we have found code to invoke the given [element], return the |
| 1198 * error code that should be reported, or `null` if no error should be | 1198 * error code that should be reported, or `null` if no error should be |
| 1199 * reported. The [target] is the target of the invocation, or `null` if there | 1199 * reported. The [target] is the target of the invocation, or `null` if there |
| 1200 * was no target. The flag [useStaticContext] should be `true` if the | 1200 * was no target. The flag [useStaticContext] should be `true` if the |
| 1201 * invocation is in a static constant (does not have access to instance state. | 1201 * invocation is in a static constant (does not have access to instance state. |
| 1202 */ | 1202 */ |
| (...skipping 1007 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2210 // identifier. Consider re-writing the AST. | 2210 // identifier. Consider re-writing the AST. |
| 2211 return element; | 2211 return element; |
| 2212 } | 2212 } |
| 2213 } | 2213 } |
| 2214 } | 2214 } |
| 2215 // TODO(brianwilkerson) Report this error. | 2215 // TODO(brianwilkerson) Report this error. |
| 2216 return null; | 2216 return null; |
| 2217 } | 2217 } |
| 2218 | 2218 |
| 2219 /** | 2219 /** |
| 2220 * Given a [node] that can have annotations associated with it, resolve the | |
| 2221 * annotations in the element model representing annotations to the node. | |
| 2222 */ | |
| 2223 void _resolveMetadataForParameter( | |
| 2224 Element element, NormalFormalParameter node) { | |
| 2225 _resolveAnnotations(node.metadata); | |
| 2226 } | |
| 2227 | |
| 2228 /** | |
| 2229 * Given that we are accessing a property of the given [targetType] with the | 2220 * Given that we are accessing a property of the given [targetType] with the |
| 2230 * given [propertyName], return the element that represents the property. The | 2221 * given [propertyName], return the element that represents the property. The |
| 2231 * [target] is the target of the invocation ('e'). | 2222 * [target] is the target of the invocation ('e'). |
| 2232 */ | 2223 */ |
| 2233 ExecutableElement _resolveProperty( | 2224 ExecutableElement _resolveProperty( |
| 2234 Expression target, DartType targetType, SimpleIdentifier propertyName) { | 2225 Expression target, DartType targetType, SimpleIdentifier propertyName) { |
| 2235 ExecutableElement memberElement = null; | 2226 ExecutableElement memberElement = null; |
| 2236 if (propertyName.inSetterContext()) { | 2227 if (propertyName.inSetterContext()) { |
| 2237 memberElement = _lookUpSetter(target, targetType, propertyName.name); | 2228 memberElement = _lookUpSetter(target, targetType, propertyName.name); |
| 2238 } | 2229 } |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2435 DartType bound = type.element.bound; | 2426 DartType bound = type.element.bound; |
| 2436 if (bound == null) { | 2427 if (bound == null) { |
| 2437 return _resolver.typeProvider.objectType; | 2428 return _resolver.typeProvider.objectType; |
| 2438 } | 2429 } |
| 2439 return bound; | 2430 return bound; |
| 2440 } | 2431 } |
| 2441 return type; | 2432 return type; |
| 2442 } | 2433 } |
| 2443 | 2434 |
| 2444 /** | 2435 /** |
| 2436 * Given a [node] that can have annotations associated with it and the |
| 2437 * [element] to which that node has been resolved, create the annotations in |
| 2438 * the element model representing the annotations on the node. |
| 2439 */ |
| 2440 void _setMetadataForParameter(Element element, NormalFormalParameter node) { |
| 2441 if (element is! ElementImpl) { |
| 2442 return; |
| 2443 } |
| 2444 List<ElementAnnotationImpl> annotationList = |
| 2445 new List<ElementAnnotationImpl>(); |
| 2446 _addAnnotations(annotationList, node.metadata); |
| 2447 if (!annotationList.isEmpty) { |
| 2448 (element as ElementImpl).metadata = annotationList; |
| 2449 } |
| 2450 } |
| 2451 |
| 2452 /** |
| 2445 * Return `true` if we should report an error as a result of looking up a | 2453 * Return `true` if we should report an error as a result of looking up a |
| 2446 * [member] in the given [type] and not finding any member. | 2454 * [member] in the given [type] and not finding any member. |
| 2447 */ | 2455 */ |
| 2448 bool _shouldReportMissingMember(DartType type, Element member) { | 2456 bool _shouldReportMissingMember(DartType type, Element member) { |
| 2449 if (member != null || type == null || type.isDynamic || type.isBottom) { | 2457 if (member != null || type == null || type.isDynamic || type.isBottom) { |
| 2450 return false; | 2458 return false; |
| 2451 } | 2459 } |
| 2452 return true; | 2460 return true; |
| 2453 } | 2461 } |
| 2454 | 2462 |
| 2455 /** | 2463 /** |
| 2456 * Checks whether the given [expression] is a reference to a class. If it is | 2464 * Checks whether the given [expression] is a reference to a class. If it is |
| 2457 * then the element representing the class is returned, otherwise `null` is | 2465 * then the element representing the class is returned, otherwise `null` is |
| 2458 * returned. | 2466 * returned. |
| 2459 */ | 2467 */ |
| 2460 static ClassElementImpl getTypeReference(Expression expression) { | 2468 static ClassElementImpl getTypeReference(Expression expression) { |
| 2461 if (expression is Identifier) { | 2469 if (expression is Identifier) { |
| 2462 Element staticElement = expression.staticElement; | 2470 Element staticElement = expression.staticElement; |
| 2463 if (staticElement is ClassElementImpl) { | 2471 if (staticElement is ClassElementImpl) { |
| 2464 return staticElement; | 2472 return staticElement; |
| 2465 } | 2473 } |
| 2466 } | 2474 } |
| 2467 return null; | 2475 return null; |
| 2468 } | 2476 } |
| 2469 | 2477 |
| 2470 /** | 2478 /** |
| 2471 * Given a [node] that can have annotations associated with it, resolve the | 2479 * Given a [node] that can have annotations associated with it and the |
| 2472 * annotations in the element model representing the annotations on the node. | 2480 * [element] to which that node has been resolved, create the annotations in |
| 2481 * the element model representing the annotations on the node. |
| 2473 */ | 2482 */ |
| 2474 static void resolveMetadata(AnnotatedNode node) { | 2483 static void setMetadata(Element element, AnnotatedNode node) { |
| 2475 _resolveAnnotations(node.metadata); | 2484 if (element is! ElementImpl) { |
| 2485 return; |
| 2486 } |
| 2487 List<ElementAnnotationImpl> annotationList = <ElementAnnotationImpl>[]; |
| 2488 _addAnnotations(annotationList, node.metadata); |
| 2476 if (node is VariableDeclaration && node.parent is VariableDeclarationList) { | 2489 if (node is VariableDeclaration && node.parent is VariableDeclarationList) { |
| 2477 VariableDeclarationList list = node.parent as VariableDeclarationList; | 2490 VariableDeclarationList list = node.parent as VariableDeclarationList; |
| 2478 _resolveAnnotations(list.metadata); | 2491 _addAnnotations(annotationList, list.metadata); |
| 2479 if (list.parent is FieldDeclaration) { | 2492 if (list.parent is FieldDeclaration) { |
| 2480 FieldDeclaration fieldDeclaration = list.parent as FieldDeclaration; | 2493 FieldDeclaration fieldDeclaration = list.parent as FieldDeclaration; |
| 2481 _resolveAnnotations(fieldDeclaration.metadata); | 2494 _addAnnotations(annotationList, fieldDeclaration.metadata); |
| 2482 } else if (list.parent is TopLevelVariableDeclaration) { | 2495 } else if (list.parent is TopLevelVariableDeclaration) { |
| 2483 TopLevelVariableDeclaration variableDeclaration = | 2496 TopLevelVariableDeclaration variableDeclaration = |
| 2484 list.parent as TopLevelVariableDeclaration; | 2497 list.parent as TopLevelVariableDeclaration; |
| 2485 _resolveAnnotations(variableDeclaration.metadata); | 2498 _addAnnotations(annotationList, variableDeclaration.metadata); |
| 2499 } |
| 2500 } |
| 2501 if (!annotationList.isEmpty) { |
| 2502 (element as ElementImpl).metadata = annotationList; |
| 2503 } |
| 2504 } |
| 2505 |
| 2506 /** |
| 2507 * Generate annotation elements for each of the annotations in the |
| 2508 * [annotationList] and add them to the given list of [annotations]. |
| 2509 */ |
| 2510 static void _addAnnotations(List<ElementAnnotationImpl> annotationList, |
| 2511 NodeList<Annotation> annotations) { |
| 2512 int annotationCount = annotations.length; |
| 2513 for (int i = 0; i < annotationCount; i++) { |
| 2514 Annotation annotation = annotations[i]; |
| 2515 Element resolvedElement = annotation.element; |
| 2516 if (resolvedElement != null) { |
| 2517 ElementAnnotationImpl elementAnnotation = |
| 2518 new ElementAnnotationImpl(resolvedElement); |
| 2519 annotation.elementAnnotation = elementAnnotation; |
| 2520 annotationList.add(elementAnnotation); |
| 2486 } | 2521 } |
| 2487 } | 2522 } |
| 2488 } | 2523 } |
| 2489 | 2524 |
| 2490 /** | 2525 /** |
| 2491 * Return `true` if the given [identifier] is the return type of a constructor | 2526 * Return `true` if the given [identifier] is the return type of a constructor |
| 2492 * declaration. | 2527 * declaration. |
| 2493 */ | 2528 */ |
| 2494 static bool _isConstructorReturnType(SimpleIdentifier identifier) { | 2529 static bool _isConstructorReturnType(SimpleIdentifier identifier) { |
| 2495 AstNode parent = identifier.parent; | 2530 AstNode parent = identifier.parent; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 2526 } | 2561 } |
| 2527 if (node is ConstructorFieldInitializer) { | 2562 if (node is ConstructorFieldInitializer) { |
| 2528 return false; | 2563 return false; |
| 2529 } | 2564 } |
| 2530 if (node is MethodDeclaration) { | 2565 if (node is MethodDeclaration) { |
| 2531 return !node.isStatic; | 2566 return !node.isStatic; |
| 2532 } | 2567 } |
| 2533 } | 2568 } |
| 2534 return false; | 2569 return false; |
| 2535 } | 2570 } |
| 2536 | |
| 2537 /** | |
| 2538 * Resolve each of the annotations in the given list of [annotations]. | |
| 2539 */ | |
| 2540 static void _resolveAnnotations(NodeList<Annotation> annotations) { | |
| 2541 for (Annotation annotation in annotations) { | |
| 2542 ElementAnnotationImpl elementAnnotation = annotation.elementAnnotation; | |
| 2543 elementAnnotation.element = annotation.element; | |
| 2544 } | |
| 2545 } | |
| 2546 } | 2571 } |
| 2547 | 2572 |
| 2548 /** | 2573 /** |
| 2549 * An identifier that can be used to look up names in the lexical scope when | 2574 * An identifier that can be used to look up names in the lexical scope when |
| 2550 * there is no identifier in the AST structure. There is no identifier in the | 2575 * there is no identifier in the AST structure. There is no identifier in the |
| 2551 * AST when the parser could not distinguish between a method invocation and an | 2576 * AST when the parser could not distinguish between a method invocation and an |
| 2552 * invocation of a top-level function imported with a prefix. | 2577 * invocation of a top-level function imported with a prefix. |
| 2553 */ | 2578 */ |
| 2554 class SyntheticIdentifier extends IdentifierImpl { | 2579 class SyntheticIdentifier extends IdentifierImpl { |
| 2555 /** | 2580 /** |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2599 | 2624 |
| 2600 @override | 2625 @override |
| 2601 Element get staticElement => null; | 2626 Element get staticElement => null; |
| 2602 | 2627 |
| 2603 @override | 2628 @override |
| 2604 accept(AstVisitor visitor) => null; | 2629 accept(AstVisitor visitor) => null; |
| 2605 | 2630 |
| 2606 @override | 2631 @override |
| 2607 void visitChildren(AstVisitor visitor) {} | 2632 void visitChildren(AstVisitor visitor) {} |
| 2608 } | 2633 } |
| OLD | NEW |