Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: pkg/analyzer/lib/src/generated/declaration_resolver.dart

Issue 2814063003: Support for using generic function types in other AST structures. (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | pkg/analyzer/test/generated/declaration_resolver_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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';
11 import 'package:analyzer/dart/element/type.dart';
11 import 'package:analyzer/exception/exception.dart'; 12 import 'package:analyzer/exception/exception.dart';
12 import 'package:analyzer/src/dart/ast/ast.dart'; 13 import 'package:analyzer/src/dart/ast/ast.dart';
13 import 'package:analyzer/src/dart/element/builder.dart'; 14 import 'package:analyzer/src/dart/element/builder.dart';
14 import 'package:analyzer/src/dart/element/element.dart'; 15 import 'package:analyzer/src/dart/element/element.dart';
15 import 'package:analyzer/src/dart/element/type.dart'; 16 import 'package:analyzer/src/dart/element/type.dart';
16 import 'package:analyzer/src/generated/resolver.dart'; 17 import 'package:analyzer/src/generated/resolver.dart';
17 18
18 /** 19 /**
19 * A visitor that resolves declarations in an AST structure to already built 20 * A visitor that resolves declarations in an AST structure to already built
20 * elements. 21 * elements.
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 if (_walker.element is ExecutableElement) { 219 if (_walker.element is ExecutableElement) {
219 element = _match(functionName, _walker.getFunction()); 220 element = _match(functionName, _walker.getFunction());
220 } else if (property.keyword == Keyword.GET) { 221 } else if (property.keyword == Keyword.GET) {
221 element = _match(functionName, _walker.getAccessor()); 222 element = _match(functionName, _walker.getAccessor());
222 } else { 223 } else {
223 assert(property.keyword == Keyword.SET); 224 assert(property.keyword == Keyword.SET);
224 element = _match(functionName, _walker.getAccessor(), 225 element = _match(functionName, _walker.getAccessor(),
225 elementName: functionName.name + '='); 226 elementName: functionName.name + '=');
226 } 227 }
227 } 228 }
229 _setGenericFunctionType(node.returnType, element.returnType);
228 node.functionExpression.element = element; 230 node.functionExpression.element = element;
229 _walker._elementHolder?.addFunction(element); 231 _walker._elementHolder?.addFunction(element);
230 _walk(new ElementWalker.forExecutable(element, _enclosingUnit), () { 232 _walk(new ElementWalker.forExecutable(element, _enclosingUnit), () {
231 super.visitFunctionDeclaration(node); 233 super.visitFunctionDeclaration(node);
232 }); 234 });
233 _resolveMetadata(node, node.metadata, element); 235 _resolveMetadata(node, node.metadata, element);
234 return null; 236 return null;
235 } 237 }
236 238
237 @override 239 @override
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 _resolveMetadata(node, node.metadata, element); 273 _resolveMetadata(node, node.metadata, element);
272 return null; 274 return null;
273 } else { 275 } else {
274 return super.visitFunctionTypedFormalParameter(node); 276 return super.visitFunctionTypedFormalParameter(node);
275 } 277 }
276 } 278 }
277 279
278 @override 280 @override
279 Object visitGenericFunctionType(GenericFunctionType node) { 281 Object visitGenericFunctionType(GenericFunctionType node) {
280 GenericFunctionTypeElement element = node.type.element; 282 GenericFunctionTypeElement element = node.type.element;
283 _setGenericFunctionType(node.returnType, element.returnType);
281 _walk(new ElementWalker.forGenericFunctionType(element), () { 284 _walk(new ElementWalker.forGenericFunctionType(element), () {
282 super.visitGenericFunctionType(node); 285 super.visitGenericFunctionType(node);
283 }); 286 });
284 return null; 287 return null;
285 } 288 }
286 289
287 @override 290 @override
288 Object visitGenericTypeAlias(GenericTypeAlias node) { 291 Object visitGenericTypeAlias(GenericTypeAlias node) {
289 GenericTypeAliasElementImpl element = 292 GenericTypeAliasElementImpl element =
290 _match(node.name, _walker.getTypedef()); 293 _match(node.name, _walker.getTypedef());
291 (node.functionType as GenericFunctionTypeImpl)?.type = 294 _setGenericFunctionType(node.functionType, element.function?.type);
292 element.function?.type;
293 _walk(new ElementWalker.forGenericTypeAlias(element), () { 295 _walk(new ElementWalker.forGenericTypeAlias(element), () {
294 super.visitGenericTypeAlias(node); 296 super.visitGenericTypeAlias(node);
295 }); 297 });
296 _resolveMetadata(node, node.metadata, element); 298 _resolveMetadata(node, node.metadata, element);
297 return null; 299 return null;
298 } 300 }
299 301
300 @override 302 @override
301 Object visitImportDirective(ImportDirective node) { 303 Object visitImportDirective(ImportDirective node) {
302 super.visitImportDirective(node); 304 super.visitImportDirective(node);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 _match(methodName, _walker.getFunction(), elementName: elementName); 352 _match(methodName, _walker.getFunction(), elementName: elementName);
351 } else { 353 } else {
352 if (property.keyword == Keyword.GET) { 354 if (property.keyword == Keyword.GET) {
353 element = _match(methodName, _walker.getAccessor()); 355 element = _match(methodName, _walker.getAccessor());
354 } else { 356 } else {
355 assert(property.keyword == Keyword.SET); 357 assert(property.keyword == Keyword.SET);
356 element = _match(methodName, _walker.getAccessor(), 358 element = _match(methodName, _walker.getAccessor(),
357 elementName: nameOfMethod + '='); 359 elementName: nameOfMethod + '=');
358 } 360 }
359 } 361 }
362 _setGenericFunctionType(node.returnType, element.returnType);
360 _walk(new ElementWalker.forExecutable(element, _enclosingUnit), () { 363 _walk(new ElementWalker.forExecutable(element, _enclosingUnit), () {
361 super.visitMethodDeclaration(node); 364 super.visitMethodDeclaration(node);
362 }); 365 });
363 _resolveMetadata(node, node.metadata, element); 366 _resolveMetadata(node, node.metadata, element);
364 return null; 367 return null;
365 } 368 }
366 369
367 @override 370 @override
368 Object visitPartDirective(PartDirective node) { 371 Object visitPartDirective(PartDirective node) {
369 super.visitPartDirective(node); 372 super.visitPartDirective(node);
(...skipping 16 matching lines...) Expand all
386 node.element = _enclosingUnit.library; 389 node.element = _enclosingUnit.library;
387 return super.visitPartOfDirective(node); 390 return super.visitPartOfDirective(node);
388 } 391 }
389 392
390 @override 393 @override
391 Object visitSimpleFormalParameter(SimpleFormalParameter node) { 394 Object visitSimpleFormalParameter(SimpleFormalParameter node) {
392 if (node.parent is! DefaultFormalParameter) { 395 if (node.parent is! DefaultFormalParameter) {
393 ParameterElement element = 396 ParameterElement element =
394 _match(node.identifier, _walker.getParameter()); 397 _match(node.identifier, _walker.getParameter());
395 (node as SimpleFormalParameterImpl).element = element; 398 (node as SimpleFormalParameterImpl).element = element;
396 TypeAnnotation type = node.type; 399 _setGenericFunctionType(node.type, element.type);
397 if (type is GenericFunctionTypeImpl) {
398 type.type = element.type;
399 }
400 _walk(new ElementWalker.forParameter(element), () { 400 _walk(new ElementWalker.forParameter(element), () {
401 super.visitSimpleFormalParameter(node); 401 super.visitSimpleFormalParameter(node);
402 }); 402 });
403 _resolveMetadata(node, node.metadata, element); 403 _resolveMetadata(node, node.metadata, element);
404 return null; 404 return null;
405 } else { 405 } else {
406 return super.visitSimpleFormalParameter(node); 406 return super.visitSimpleFormalParameter(node);
407 } 407 }
408 } 408 }
409 409
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 } else { 456 } else {
457 return super.visitVariableDeclaration(node); 457 return super.visitVariableDeclaration(node);
458 } 458 }
459 } 459 }
460 460
461 @override 461 @override
462 Object visitVariableDeclarationList(VariableDeclarationList node) { 462 Object visitVariableDeclarationList(VariableDeclarationList node) {
463 if (_walker.elementBuilder != null) { 463 if (_walker.elementBuilder != null) {
464 return _walker.elementBuilder.visitVariableDeclarationList(node); 464 return _walker.elementBuilder.visitVariableDeclarationList(node);
465 } else { 465 } else {
466 super.visitVariableDeclarationList(node); 466 node.variables.accept(this);
467 VariableElement firstVariable = node.variables[0].element;
468 _setGenericFunctionType(node.type, firstVariable.type);
469 node.type?.accept(this);
467 if (node.parent is! FieldDeclaration && 470 if (node.parent is! FieldDeclaration &&
468 node.parent is! TopLevelVariableDeclaration) { 471 node.parent is! TopLevelVariableDeclaration) {
469 _resolveMetadata(node, node.metadata, node.variables[0].element); 472 _resolveMetadata(node, node.metadata, firstVariable);
470 } 473 }
471 return null; 474 return null;
472 } 475 }
473 } 476 }
474 477
475 /** 478 /**
476 * Updates [identifier] to point to [element], after ensuring that the 479 * Updates [identifier] to point to [element], after ensuring that the
477 * element has the expected name. 480 * element has the expected name.
478 * 481 *
479 * If no [elementName] is given, it defaults to the name of the [identifier] 482 * If no [elementName] is given, it defaults to the name of the [identifier]
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 * built. 534 * built.
532 */ 535 */
533 void _resolveMetadata( 536 void _resolveMetadata(
534 AstNode parent, NodeList<Annotation> nodes, Element element) { 537 AstNode parent, NodeList<Annotation> nodes, Element element) {
535 if (element != null) { 538 if (element != null) {
536 _resolveAnnotations(parent, nodes, element.metadata); 539 _resolveAnnotations(parent, nodes, element.metadata);
537 } 540 }
538 } 541 }
539 542
540 /** 543 /**
544 * If the given [typeNode] is a [GenericFunctionType], set its [type].
545 */
546 void _setGenericFunctionType(TypeAnnotation typeNode, DartType type) {
547 if (typeNode is GenericFunctionTypeImpl) {
548 typeNode.type = type;
549 }
550 }
551
552 /**
541 * Recurses through the element model and AST, verifying that all elements are 553 * Recurses through the element model and AST, verifying that all elements are
542 * matched. 554 * matched.
543 * 555 *
544 * Executes [callback] with [_walker] pointing to the given [walker] (which 556 * Executes [callback] with [_walker] pointing to the given [walker] (which
545 * should be a new instance of [ElementWalker]). Once [callback] returns, 557 * should be a new instance of [ElementWalker]). Once [callback] returns,
546 * uses [ElementWalker.validate] to verify that all expected elements have 558 * uses [ElementWalker.validate] to verify that all expected elements have
547 * been matched. 559 * been matched.
548 */ 560 */
549 void _walk(ElementWalker walker, void callback()) { 561 void _walk(ElementWalker walker, void callback()) {
550 ElementWalker outerWalker = _walker; 562 ElementWalker outerWalker = _walker;
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
782 class _ElementMismatchException extends AnalysisException { 794 class _ElementMismatchException extends AnalysisException {
783 /** 795 /**
784 * Creates an exception to refer to the given [compilationUnit], [element], 796 * Creates an exception to refer to the given [compilationUnit], [element],
785 * and [cause]. 797 * and [cause].
786 */ 798 */
787 _ElementMismatchException( 799 _ElementMismatchException(
788 CompilationUnitElement compilationUnit, Element element, 800 CompilationUnitElement compilationUnit, Element element,
789 [CaughtException cause = null]) 801 [CaughtException cause = null])
790 : super('Element mismatch in $compilationUnit at $element', cause); 802 : super('Element mismatch in $compilationUnit at $element', cause);
791 } 803 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/generated/declaration_resolver_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698