OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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.declaration_resolver; | 5 library analyzer.src.generated.declaration_resolver; |
6 | 6 |
7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
8 import 'package:analyzer/dart/ast/token.dart'; | 8 import 'package:analyzer/dart/ast/token.dart'; |
9 import 'package:analyzer/dart/ast/visitor.dart'; | 9 import 'package:analyzer/dart/ast/visitor.dart'; |
10 import 'package:analyzer/dart/element/element.dart'; | 10 import 'package:analyzer/dart/element/element.dart'; |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 _walker.elementBuilder.visitDeclaredIdentifier(node); | 120 _walker.elementBuilder.visitDeclaredIdentifier(node); |
121 return null; | 121 return null; |
122 } | 122 } |
123 | 123 |
124 @override | 124 @override |
125 Object visitDefaultFormalParameter(DefaultFormalParameter node) { | 125 Object visitDefaultFormalParameter(DefaultFormalParameter node) { |
126 NormalFormalParameter normalParameter = node.parameter; | 126 NormalFormalParameter normalParameter = node.parameter; |
127 ParameterElement element = | 127 ParameterElement element = |
128 _match(normalParameter.identifier, _walker.getParameter()); | 128 _match(normalParameter.identifier, _walker.getParameter()); |
129 if (normalParameter is SimpleFormalParameterImpl) { | 129 if (normalParameter is SimpleFormalParameterImpl) { |
130 normalParameter.element = node.identifier.staticElement; | 130 normalParameter.element = element; |
131 } | 131 } |
132 Expression defaultValue = node.defaultValue; | 132 Expression defaultValue = node.defaultValue; |
133 if (defaultValue != null) { | 133 if (defaultValue != null) { |
134 _walk( | 134 _walk( |
135 new ElementWalker.forExecutable(element.initializer, _enclosingUnit), | 135 new ElementWalker.forExecutable(element.initializer, _enclosingUnit), |
136 () { | 136 () { |
137 defaultValue.accept(this); | 137 defaultValue.accept(this); |
138 }); | 138 }); |
139 } | 139 } |
140 _walk(new ElementWalker.forParameter(element), () { | 140 _walk(new ElementWalker.forParameter(element), () { |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 super.visitFunctionTypedFormalParameter(node); | 269 super.visitFunctionTypedFormalParameter(node); |
270 }); | 270 }); |
271 _resolveMetadata(node, node.metadata, element); | 271 _resolveMetadata(node, node.metadata, element); |
272 return null; | 272 return null; |
273 } else { | 273 } else { |
274 return super.visitFunctionTypedFormalParameter(node); | 274 return super.visitFunctionTypedFormalParameter(node); |
275 } | 275 } |
276 } | 276 } |
277 | 277 |
278 @override | 278 @override |
| 279 Object visitGenericFunctionType(GenericFunctionType node) { |
| 280 GenericFunctionTypeElement element = node.type.element; |
| 281 _walk(new ElementWalker.forGenericFunctionType(element), () { |
| 282 super.visitGenericFunctionType(node); |
| 283 }); |
| 284 return null; |
| 285 } |
| 286 |
| 287 @override |
| 288 Object visitGenericTypeAlias(GenericTypeAlias node) { |
| 289 GenericTypeAliasElementImpl element = |
| 290 _match(node.name, _walker.getTypedef()); |
| 291 (node.functionType as GenericFunctionTypeImpl)?.type = |
| 292 element.function?.type; |
| 293 _walk(new ElementWalker.forGenericTypeAlias(element), () { |
| 294 super.visitGenericTypeAlias(node); |
| 295 }); |
| 296 _resolveMetadata(node, node.metadata, element); |
| 297 return null; |
| 298 } |
| 299 |
| 300 @override |
279 Object visitImportDirective(ImportDirective node) { | 301 Object visitImportDirective(ImportDirective node) { |
280 super.visitImportDirective(node); | 302 super.visitImportDirective(node); |
281 List<ElementAnnotation> annotations = | 303 List<ElementAnnotation> annotations = |
282 _enclosingUnit.getAnnotations(node.offset); | 304 _enclosingUnit.getAnnotations(node.offset); |
283 if (annotations.isEmpty && node.metadata.isNotEmpty) { | 305 if (annotations.isEmpty && node.metadata.isNotEmpty) { |
284 int index = (node.parent as CompilationUnit) | 306 int index = (node.parent as CompilationUnit) |
285 .directives | 307 .directives |
286 .where((directive) => directive is ImportDirective) | 308 .where((directive) => directive is ImportDirective) |
287 .toList() | 309 .toList() |
288 .indexOf(node); | 310 .indexOf(node); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 Object visitPartOfDirective(PartOfDirective node) { | 385 Object visitPartOfDirective(PartOfDirective node) { |
364 node.element = _enclosingUnit.library; | 386 node.element = _enclosingUnit.library; |
365 return super.visitPartOfDirective(node); | 387 return super.visitPartOfDirective(node); |
366 } | 388 } |
367 | 389 |
368 @override | 390 @override |
369 Object visitSimpleFormalParameter(SimpleFormalParameter node) { | 391 Object visitSimpleFormalParameter(SimpleFormalParameter node) { |
370 if (node.parent is! DefaultFormalParameter) { | 392 if (node.parent is! DefaultFormalParameter) { |
371 ParameterElement element = | 393 ParameterElement element = |
372 _match(node.identifier, _walker.getParameter()); | 394 _match(node.identifier, _walker.getParameter()); |
373 (node as SimpleFormalParameterImpl).element = | 395 (node as SimpleFormalParameterImpl).element = element; |
374 node.identifier.staticElement; | 396 TypeAnnotation type = node.type; |
| 397 if (type is GenericFunctionTypeImpl) { |
| 398 type.type = element.type; |
| 399 } |
375 _walk(new ElementWalker.forParameter(element), () { | 400 _walk(new ElementWalker.forParameter(element), () { |
376 super.visitSimpleFormalParameter(node); | 401 super.visitSimpleFormalParameter(node); |
377 }); | 402 }); |
378 _resolveMetadata(node, node.metadata, element); | 403 _resolveMetadata(node, node.metadata, element); |
379 return null; | 404 return null; |
380 } else { | 405 } else { |
381 return super.visitSimpleFormalParameter(node); | 406 return super.visitSimpleFormalParameter(node); |
382 } | 407 } |
383 } | 408 } |
384 | 409 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 * If no [elementName] is given, it defaults to the name of the [identifier] | 479 * If no [elementName] is given, it defaults to the name of the [identifier] |
455 * (or the empty string if [identifier] is `null`). | 480 * (or the empty string if [identifier] is `null`). |
456 * | 481 * |
457 * If [identifier] is `null`, nothing is updated, but the element name is | 482 * If [identifier] is `null`, nothing is updated, but the element name is |
458 * still checked. | 483 * still checked. |
459 */ | 484 */ |
460 Element/*=E*/ _match/*<E extends Element>*/( | 485 Element/*=E*/ _match/*<E extends Element>*/( |
461 SimpleIdentifier identifier, Element/*=E*/ element, | 486 SimpleIdentifier identifier, Element/*=E*/ element, |
462 {String elementName, int offset}) { | 487 {String elementName, int offset}) { |
463 elementName ??= identifier?.name ?? ''; | 488 elementName ??= identifier?.name ?? ''; |
464 offset ??= identifier.offset; | 489 offset ??= identifier?.offset ?? -1; |
465 if (element.name != elementName) { | 490 if (element.name != elementName) { |
466 throw new StateError( | 491 throw new StateError( |
467 'Expected an element matching `$elementName`, got `${element.name}`'); | 492 'Expected an element matching `$elementName`, got `${element.name}`'); |
468 } | 493 } |
469 identifier?.staticElement = element; | 494 identifier?.staticElement = element; |
470 _matchOffset(element, offset); | 495 _matchOffset(element, offset); |
471 return element; | 496 return element; |
472 } | 497 } |
473 | 498 |
474 void _matchOffset(Element element, int offset) { | 499 void _matchOffset(Element element, int offset) { |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
610 | 635 |
611 /** | 636 /** |
612 * Creates an [ElementWalker] which walks the child elements of a compilation | 637 * Creates an [ElementWalker] which walks the child elements of a compilation |
613 * unit element. | 638 * unit element. |
614 */ | 639 */ |
615 ElementWalker.forExecutable( | 640 ElementWalker.forExecutable( |
616 ExecutableElement element, CompilationUnitElement compilationUnit) | 641 ExecutableElement element, CompilationUnitElement compilationUnit) |
617 : this._forExecutable(element, compilationUnit, new ElementHolder()); | 642 : this._forExecutable(element, compilationUnit, new ElementHolder()); |
618 | 643 |
619 /** | 644 /** |
| 645 * Creates an [ElementWalker] which walks the child elements of a typedef |
| 646 * element. |
| 647 */ |
| 648 ElementWalker.forGenericFunctionType(GenericFunctionTypeElement element) |
| 649 : element = element, |
| 650 _parameters = element.parameters, |
| 651 _typeParameters = element.typeParameters; |
| 652 |
| 653 /** |
| 654 * Creates an [ElementWalker] which walks the child elements of a typedef |
| 655 * element defined using a generic function type. |
| 656 */ |
| 657 ElementWalker.forGenericTypeAlias(FunctionTypeAliasElement element) |
| 658 : element = element, |
| 659 _typeParameters = element.typeParameters; |
| 660 |
| 661 /** |
620 * Creates an [ElementWalker] which walks the child elements of a parameter | 662 * Creates an [ElementWalker] which walks the child elements of a parameter |
621 * element. | 663 * element. |
622 */ | 664 */ |
623 ElementWalker.forParameter(ParameterElement element) | 665 ElementWalker.forParameter(ParameterElement element) |
624 : element = element, | 666 : element = element, |
625 _parameters = element.parameters, | 667 _parameters = element.parameters, |
626 _typeParameters = element.typeParameters; | 668 _typeParameters = element.typeParameters; |
627 | 669 |
628 /** | 670 /** |
629 * Creates an [ElementWalker] which walks the child elements of a typedef | 671 * Creates an [ElementWalker] which walks the child elements of a typedef |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
740 class _ElementMismatchException extends AnalysisException { | 782 class _ElementMismatchException extends AnalysisException { |
741 /** | 783 /** |
742 * Creates an exception to refer to the given [compilationUnit], [element], | 784 * Creates an exception to refer to the given [compilationUnit], [element], |
743 * and [cause]. | 785 * and [cause]. |
744 */ | 786 */ |
745 _ElementMismatchException( | 787 _ElementMismatchException( |
746 CompilationUnitElement compilationUnit, Element element, | 788 CompilationUnitElement compilationUnit, Element element, |
747 [CaughtException cause = null]) | 789 [CaughtException cause = null]) |
748 : super('Element mismatch in $compilationUnit at $element', cause); | 790 : super('Element mismatch in $compilationUnit at $element', cause); |
749 } | 791 } |
OLD | NEW |