Chromium Code Reviews| 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/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/type.dart' show DartType; | 10 import 'package:analyzer/dart/element/type.dart' show DartType; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 30 | 30 |
| 31 /** | 31 /** |
| 32 * If the expression being serialized can contain closures, map whose | 32 * If the expression being serialized can contain closures, map whose |
| 33 * keys are the offsets of local function nodes representing those closures, | 33 * keys are the offsets of local function nodes representing those closures, |
| 34 * and whose values are indices of those local functions relative to their | 34 * and whose values are indices of those local functions relative to their |
| 35 * siblings. | 35 * siblings. |
| 36 */ | 36 */ |
| 37 final Map<int, int> localClosureIndexMap; | 37 final Map<int, int> localClosureIndexMap; |
| 38 | 38 |
| 39 /** | 39 /** |
| 40 * If a constructor initializer expression is being serialized, the names of | 40 * If the expression being serialize appears inside a function body, the names |
|
scheglov
2016/05/26 16:59:33
"being serialized"?
Paul Berry
2016/05/26 17:04:23
Fixed, thanks.
| |
| 41 * the constructor parameters. Otherwise `null`. | 41 * of parameters that are in scope. Otherwise `null`. |
| 42 */ | 42 */ |
| 43 final Set<String> constructorParameterNames; | 43 final Set<String> parameterNames; |
| 44 | 44 |
| 45 _ConstExprSerializer( | 45 _ConstExprSerializer( |
| 46 this.visitor, this.localClosureIndexMap, this.constructorParameterNames); | 46 this.visitor, this.localClosureIndexMap, this.parameterNames); |
| 47 | 47 |
| 48 @override | 48 @override |
| 49 bool isConstructorParameterName(String name) { | 49 bool isParameterName(String name) { |
| 50 return constructorParameterNames?.contains(name) ?? false; | 50 return parameterNames?.contains(name) ?? false; |
| 51 } | 51 } |
| 52 | 52 |
| 53 @override | 53 @override |
| 54 void serializeAnnotation(Annotation annotation) { | 54 void serializeAnnotation(Annotation annotation) { |
| 55 if (annotation.arguments == null) { | 55 if (annotation.arguments == null) { |
| 56 assert(annotation.constructorName == null); | 56 assert(annotation.constructorName == null); |
| 57 serialize(annotation.name); | 57 serialize(annotation.name); |
| 58 } else { | 58 } else { |
| 59 Identifier name = annotation.name; | 59 Identifier name = annotation.name; |
| 60 EntityRefBuilder constructor; | 60 EntityRefBuilder constructor; |
| (...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 346 Map<int, int> _localClosureIndexMap; | 346 Map<int, int> _localClosureIndexMap; |
| 347 | 347 |
| 348 /** | 348 /** |
| 349 * Indicates whether closure function bodies should be serialized. This flag | 349 * Indicates whether closure function bodies should be serialized. This flag |
| 350 * is set while visiting the bodies of initializer expressions that will be | 350 * is set while visiting the bodies of initializer expressions that will be |
| 351 * needed by type inference. | 351 * needed by type inference. |
| 352 */ | 352 */ |
| 353 bool _serializeClosureBodyExprs = false; | 353 bool _serializeClosureBodyExprs = false; |
| 354 | 354 |
| 355 /** | 355 /** |
| 356 * If a closure function body is being serialized, the set of closure | |
| 357 * parameter names which are currently in scope. Otherwise `null`. | |
| 358 */ | |
| 359 Set<String> _parameterNames; | |
| 360 | |
| 361 /** | |
| 356 * Create a slot id for storing a propagated or inferred type or const cycle | 362 * Create a slot id for storing a propagated or inferred type or const cycle |
| 357 * info. | 363 * info. |
| 358 */ | 364 */ |
| 359 int assignSlot() => ++numSlots; | 365 int assignSlot() => ++numSlots; |
| 360 | 366 |
| 361 /** | 367 /** |
| 362 * Build a [_Scope] object containing the names defined within the body of a | 368 * Build a [_Scope] object containing the names defined within the body of a |
| 363 * class declaration. | 369 * class declaration. |
| 364 */ | 370 */ |
| 365 _Scope buildClassMemberScope( | 371 _Scope buildClassMemberScope( |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 522 b.variables = variables; | 528 b.variables = variables; |
| 523 b.publicNamespace = computePublicNamespace(compilationUnit); | 529 b.publicNamespace = computePublicNamespace(compilationUnit); |
| 524 return b; | 530 return b; |
| 525 } | 531 } |
| 526 | 532 |
| 527 /** | 533 /** |
| 528 * Serialize the given [expression], creating an [UnlinkedConstBuilder]. | 534 * Serialize the given [expression], creating an [UnlinkedConstBuilder]. |
| 529 */ | 535 */ |
| 530 UnlinkedConstBuilder serializeConstExpr( | 536 UnlinkedConstBuilder serializeConstExpr( |
| 531 Map<int, int> localClosureIndexMap, Expression expression, | 537 Map<int, int> localClosureIndexMap, Expression expression, |
| 532 [Set<String> constructorParameterNames]) { | 538 [Set<String> parameterNames]) { |
| 533 _ConstExprSerializer serializer = new _ConstExprSerializer( | 539 _ConstExprSerializer serializer = |
| 534 this, localClosureIndexMap, constructorParameterNames); | 540 new _ConstExprSerializer(this, localClosureIndexMap, parameterNames); |
| 535 serializer.serialize(expression); | 541 serializer.serialize(expression); |
| 536 return serializer.toBuilder(); | 542 return serializer.toBuilder(); |
| 537 } | 543 } |
| 538 | 544 |
| 539 /** | 545 /** |
| 540 * Serialize the given [declaredIdentifier] into [UnlinkedVariable], and | 546 * Serialize the given [declaredIdentifier] into [UnlinkedVariable], and |
| 541 * store it in [variables]. | 547 * store it in [variables]. |
| 542 */ | 548 */ |
| 543 void serializeDeclaredIdentifier( | 549 void serializeDeclaredIdentifier( |
| 544 AstNode scopeNode, | 550 AstNode scopeNode, |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 647 } | 653 } |
| 648 } | 654 } |
| 649 b.documentationComment = serializeDocumentation(documentationComment); | 655 b.documentationComment = serializeDocumentation(documentationComment); |
| 650 b.annotations = serializeAnnotations(annotations); | 656 b.annotations = serializeAnnotations(annotations); |
| 651 b.codeRange = serializeCodeRange(node); | 657 b.codeRange = serializeCodeRange(node); |
| 652 if (returnType == null && !isSemanticallyStatic) { | 658 if (returnType == null && !isSemanticallyStatic) { |
| 653 b.inferredReturnTypeSlot = assignSlot(); | 659 b.inferredReturnTypeSlot = assignSlot(); |
| 654 } | 660 } |
| 655 b.visibleOffset = enclosingBlock?.offset; | 661 b.visibleOffset = enclosingBlock?.offset; |
| 656 b.visibleLength = enclosingBlock?.length; | 662 b.visibleLength = enclosingBlock?.length; |
| 663 Set<String> oldParameterNames = _parameterNames; | |
| 664 if (formalParameters != null && formalParameters.parameters.isNotEmpty) { | |
| 665 _parameterNames = | |
| 666 _parameterNames == null ? new Set<String>() : _parameterNames.toSet(); | |
| 667 _parameterNames.addAll(formalParameters.parameters | |
| 668 .map((FormalParameter p) => p.identifier.name)); | |
| 669 } | |
| 657 serializeFunctionBody(b, null, body, serializeBodyExpr); | 670 serializeFunctionBody(b, null, body, serializeBodyExpr); |
| 671 _parameterNames = oldParameterNames; | |
| 658 scopes.removeLast(); | 672 scopes.removeLast(); |
| 659 assert(scopes.length == oldScopesLength); | 673 assert(scopes.length == oldScopesLength); |
| 660 return b; | 674 return b; |
| 661 } | 675 } |
| 662 | 676 |
| 663 /** | 677 /** |
| 664 * Record local functions and variables into the given executable. The given | 678 * Record local functions and variables into the given executable. The given |
| 665 * [body] is usually an actual [FunctionBody], but may be an [Expression] | 679 * [body] is usually an actual [FunctionBody], but may be an [Expression] |
| 666 * when we process a synthetic variable initializer function. | 680 * when we process a synthetic variable initializer function. |
| 667 * | 681 * |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 698 _localClosureIndexMap = <int, int>{}; | 712 _localClosureIndexMap = <int, int>{}; |
| 699 _serializeClosureBodyExprs = serializeBodyExpr; | 713 _serializeClosureBodyExprs = serializeBodyExpr; |
| 700 if (initializers != null) { | 714 if (initializers != null) { |
| 701 for (ConstructorInitializer initializer in initializers) { | 715 for (ConstructorInitializer initializer in initializers) { |
| 702 initializer.accept(this); | 716 initializer.accept(this); |
| 703 } | 717 } |
| 704 } | 718 } |
| 705 body.accept(this); | 719 body.accept(this); |
| 706 if (serializeBodyExpr) { | 720 if (serializeBodyExpr) { |
| 707 if (body is Expression) { | 721 if (body is Expression) { |
| 708 b.bodyExpr = serializeConstExpr(_localClosureIndexMap, body); | 722 b.bodyExpr = |
| 723 serializeConstExpr(_localClosureIndexMap, body, _parameterNames); | |
| 709 } else if (body is ExpressionFunctionBody) { | 724 } else if (body is ExpressionFunctionBody) { |
| 710 b.bodyExpr = serializeConstExpr(_localClosureIndexMap, body.expression); | 725 b.bodyExpr = serializeConstExpr( |
| 726 _localClosureIndexMap, body.expression, _parameterNames); | |
| 711 } else { | 727 } else { |
| 712 // TODO(paulberry): serialize other types of function bodies. | 728 // TODO(paulberry): serialize other types of function bodies. |
| 713 } | 729 } |
| 714 } | 730 } |
| 715 b.localFunctions = executables; | 731 b.localFunctions = executables; |
| 716 b.localLabels = labels; | 732 b.localLabels = labels; |
| 717 b.localVariables = variables; | 733 b.localVariables = variables; |
| 718 Map<int, int> localClosureIndexMap = _localClosureIndexMap; | 734 Map<int, int> localClosureIndexMap = _localClosureIndexMap; |
| 719 executables = oldExecutables; | 735 executables = oldExecutables; |
| 720 labels = oldLabels; | 736 labels = oldLabels; |
| (...skipping 639 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1360 /** | 1376 /** |
| 1361 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s. | 1377 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s. |
| 1362 */ | 1378 */ |
| 1363 class _TypeParameterScope extends _Scope { | 1379 class _TypeParameterScope extends _Scope { |
| 1364 /** | 1380 /** |
| 1365 * Get the number of [_ScopedTypeParameter]s defined in this | 1381 * Get the number of [_ScopedTypeParameter]s defined in this |
| 1366 * [_TypeParameterScope]. | 1382 * [_TypeParameterScope]. |
| 1367 */ | 1383 */ |
| 1368 int get length => _definedNames.length; | 1384 int get length => _definedNames.length; |
| 1369 } | 1385 } |
| OLD | NEW |