| 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 serialization.summarize_ast; | 5 library serialization.summarize_ast; |
| 6 | 6 |
| 7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
| 8 import 'package:analyzer/dart/ast/visitor.dart'; | 8 import 'package:analyzer/dart/ast/visitor.dart'; |
| 9 import 'package:analyzer/src/generated/scanner.dart'; | 9 import 'package:analyzer/src/generated/scanner.dart'; |
| 10 import 'package:analyzer/src/generated/utilities_dart.dart'; | 10 import 'package:analyzer/src/generated/utilities_dart.dart'; |
| (...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 b.kind = UnlinkedExecutableKind.functionOrMethod; | 438 b.kind = UnlinkedExecutableKind.functionOrMethod; |
| 439 } | 439 } |
| 440 b.isAbstract = body is EmptyFunctionBody; | 440 b.isAbstract = body is EmptyFunctionBody; |
| 441 b.name = nameString; | 441 b.name = nameString; |
| 442 b.nameOffset = name.offset; | 442 b.nameOffset = name.offset; |
| 443 b.typeParameters = | 443 b.typeParameters = |
| 444 serializeTypeParameters(typeParameters, typeParameterScope); | 444 serializeTypeParameters(typeParameters, typeParameterScope); |
| 445 if (!isTopLevel) { | 445 if (!isTopLevel) { |
| 446 b.isStatic = isStatic; | 446 b.isStatic = isStatic; |
| 447 } | 447 } |
| 448 b.returnType = serializeTypeName(returnType, allowVoid: true); | 448 b.returnType = serializeTypeName(returnType); |
| 449 b.hasImplicitReturnType = returnType == null; | 449 b.hasImplicitReturnType = returnType == null; |
| 450 b.isExternal = isExternal; | 450 b.isExternal = isExternal; |
| 451 if (formalParameters != null) { | 451 if (formalParameters != null) { |
| 452 b.parameters = formalParameters.parameters | 452 b.parameters = formalParameters.parameters |
| 453 .map((FormalParameter p) => p.accept(this)) | 453 .map((FormalParameter p) => p.accept(this)) |
| 454 .toList(); | 454 .toList(); |
| 455 } | 455 } |
| 456 b.documentationComment = serializeDocumentation(documentationComment); | 456 b.documentationComment = serializeDocumentation(documentationComment); |
| 457 scopes.removeLast(); | 457 scopes.removeLast(); |
| 458 assert(scopes.length == oldScopesLength); | 458 assert(scopes.length == oldScopesLength); |
| 459 return b; | 459 return b; |
| 460 } | 460 } |
| 461 | 461 |
| 462 /** | 462 /** |
| 463 * Serialize the return type and parameters of a function-typed formal | 463 * Serialize the return type and parameters of a function-typed formal |
| 464 * parameter and store them in [b]. | 464 * parameter and store them in [b]. |
| 465 */ | 465 */ |
| 466 void serializeFunctionTypedParameterDetails(UnlinkedParamBuilder b, | 466 void serializeFunctionTypedParameterDetails(UnlinkedParamBuilder b, |
| 467 TypeName returnType, FormalParameterList parameters) { | 467 TypeName returnType, FormalParameterList parameters) { |
| 468 EntityRefBuilder serializedReturnType = | 468 EntityRefBuilder serializedReturnType = serializeTypeName(returnType); |
| 469 serializeTypeName(returnType, allowVoid: true); | |
| 470 if (serializedReturnType != null) { | 469 if (serializedReturnType != null) { |
| 471 b.type = serializedReturnType; | 470 b.type = serializedReturnType; |
| 472 } | 471 } |
| 473 b.parameters = parameters.parameters | 472 b.parameters = parameters.parameters |
| 474 .map((FormalParameter p) => p.accept(this)) | 473 .map((FormalParameter p) => p.accept(this)) |
| 475 .toList(); | 474 .toList(); |
| 476 } | 475 } |
| 477 | 476 |
| 478 /** | 477 /** |
| 479 * Serialize a [FieldFormalParameter], [FunctionTypedFormalParameter], or | 478 * Serialize a [FieldFormalParameter], [FunctionTypedFormalParameter], or |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 prefixReference: prefixIndex, name: name)); | 512 prefixReference: prefixIndex, name: name)); |
| 514 return index; | 513 return index; |
| 515 }); | 514 }); |
| 516 | 515 |
| 517 /** | 516 /** |
| 518 * Serialize a type name (which might be defined in a nested scope, at top | 517 * Serialize a type name (which might be defined in a nested scope, at top |
| 519 * level within this library, or at top level within an imported library) to | 518 * level within this library, or at top level within an imported library) to |
| 520 * a [EntityRef]. Note that this method does the right thing if the | 519 * a [EntityRef]. Note that this method does the right thing if the |
| 521 * name doesn't refer to an entity other than a type (e.g. a class member). | 520 * name doesn't refer to an entity other than a type (e.g. a class member). |
| 522 */ | 521 */ |
| 523 EntityRefBuilder serializeTypeName(TypeName node, {bool allowVoid: false}) { | 522 EntityRefBuilder serializeTypeName(TypeName node) { |
| 524 EntityRefBuilder b = new EntityRefBuilder(); | 523 EntityRefBuilder b = new EntityRefBuilder(); |
| 525 if (node == null) { | 524 if (node == null) { |
| 526 b.reference = serializeReference(null, 'dynamic'); | 525 b.reference = serializeReference(null, 'dynamic'); |
| 527 } else { | 526 } else { |
| 528 Identifier identifier = node.name; | 527 Identifier identifier = node.name; |
| 529 if (identifier is SimpleIdentifier) { | 528 if (identifier is SimpleIdentifier) { |
| 530 String name = identifier.name; | 529 String name = identifier.name; |
| 531 int indexOffset = 0; | 530 int indexOffset = 0; |
| 532 for (int i = scopes.length - 1; i >= 0; i--) { | 531 for (int i = scopes.length - 1; i >= 0; i--) { |
| 533 _Scope scope = scopes[i]; | 532 _Scope scope = scopes[i]; |
| 534 _ScopedEntity entity = scope[name]; | 533 _ScopedEntity entity = scope[name]; |
| 535 if (entity != null) { | 534 if (entity != null) { |
| 536 if (entity is _ScopedTypeParameter) { | 535 if (entity is _ScopedTypeParameter) { |
| 537 b.paramReference = indexOffset + entity.index; | 536 b.paramReference = indexOffset + entity.index; |
| 538 return b; | 537 return b; |
| 539 } else { | 538 } else { |
| 540 // None of the other things that can be declared in local scopes | 539 // None of the other things that can be declared in local scopes |
| 541 // are types, so this is an error and should be treated as a | 540 // are types, so this is an error and should be treated as a |
| 542 // reference to `dynamic`. | 541 // reference to `dynamic`. |
| 543 b.reference = serializeReference(null, 'dynamic'); | 542 b.reference = serializeReference(null, 'dynamic'); |
| 544 return b; | 543 return b; |
| 545 } | 544 } |
| 546 } | 545 } |
| 547 if (scope is _TypeParameterScope) { | 546 if (scope is _TypeParameterScope) { |
| 548 indexOffset += scope.length; | 547 indexOffset += scope.length; |
| 549 } | 548 } |
| 550 } | 549 } |
| 551 if (allowVoid && name == 'void') { | |
| 552 return null; | |
| 553 } | |
| 554 b.reference = serializeReference(null, name); | 550 b.reference = serializeReference(null, name); |
| 555 } else if (identifier is PrefixedIdentifier) { | 551 } else if (identifier is PrefixedIdentifier) { |
| 556 int prefixIndex = prefixIndices.putIfAbsent(identifier.prefix.name, | 552 int prefixIndex = prefixIndices.putIfAbsent(identifier.prefix.name, |
| 557 () => serializeReference(null, identifier.prefix.name)); | 553 () => serializeReference(null, identifier.prefix.name)); |
| 558 b.reference = | 554 b.reference = |
| 559 serializeReference(prefixIndex, identifier.identifier.name); | 555 serializeReference(prefixIndex, identifier.identifier.name); |
| 560 } else { | 556 } else { |
| 561 throw new StateError( | 557 throw new StateError( |
| 562 'Unexpected identifier type: ${identifier.runtimeType}'); | 558 'Unexpected identifier type: ${identifier.runtimeType}'); |
| 563 } | 559 } |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 755 @override | 751 @override |
| 756 void visitFunctionTypeAlias(FunctionTypeAlias node) { | 752 void visitFunctionTypeAlias(FunctionTypeAlias node) { |
| 757 int oldScopesLength = scopes.length; | 753 int oldScopesLength = scopes.length; |
| 758 _TypeParameterScope typeParameterScope = new _TypeParameterScope(); | 754 _TypeParameterScope typeParameterScope = new _TypeParameterScope(); |
| 759 scopes.add(typeParameterScope); | 755 scopes.add(typeParameterScope); |
| 760 UnlinkedTypedefBuilder b = new UnlinkedTypedefBuilder(); | 756 UnlinkedTypedefBuilder b = new UnlinkedTypedefBuilder(); |
| 761 b.name = node.name.name; | 757 b.name = node.name.name; |
| 762 b.nameOffset = node.name.offset; | 758 b.nameOffset = node.name.offset; |
| 763 b.typeParameters = | 759 b.typeParameters = |
| 764 serializeTypeParameters(node.typeParameters, typeParameterScope); | 760 serializeTypeParameters(node.typeParameters, typeParameterScope); |
| 765 EntityRefBuilder serializedReturnType = | 761 EntityRefBuilder serializedReturnType = serializeTypeName(node.returnType); |
| 766 serializeTypeName(node.returnType, allowVoid: true); | |
| 767 if (serializedReturnType != null) { | 762 if (serializedReturnType != null) { |
| 768 b.returnType = serializedReturnType; | 763 b.returnType = serializedReturnType; |
| 769 } | 764 } |
| 770 b.parameters = node.parameters.parameters | 765 b.parameters = node.parameters.parameters |
| 771 .map((FormalParameter p) => p.accept(this)) | 766 .map((FormalParameter p) => p.accept(this)) |
| 772 .toList(); | 767 .toList(); |
| 773 b.documentationComment = serializeDocumentation(node.documentationComment); | 768 b.documentationComment = serializeDocumentation(node.documentationComment); |
| 774 typedefs.add(b); | 769 typedefs.add(b); |
| 775 scopes.removeLast(); | 770 scopes.removeLast(); |
| 776 assert(scopes.length == oldScopesLength); | 771 assert(scopes.length == oldScopesLength); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 875 /** | 870 /** |
| 876 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s. | 871 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s. |
| 877 */ | 872 */ |
| 878 class _TypeParameterScope extends _Scope { | 873 class _TypeParameterScope extends _Scope { |
| 879 /** | 874 /** |
| 880 * Get the number of [_ScopedTypeParameter]s defined in this | 875 * Get the number of [_ScopedTypeParameter]s defined in this |
| 881 * [_TypeParameterScope]. | 876 * [_TypeParameterScope]. |
| 882 */ | 877 */ |
| 883 int get length => _definedNames.length; | 878 int get length => _definedNames.length; |
| 884 } | 879 } |
| OLD | NEW |