| 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_const_expr; | 5 library serialization.summarize_const_expr; |
| 6 | 6 |
| 7 import 'package:analyzer/src/generated/ast.dart'; | 7 import 'package:analyzer/src/generated/ast.dart'; |
| 8 import 'package:analyzer/src/generated/scanner.dart'; | 8 import 'package:analyzer/src/generated/scanner.dart'; |
| 9 import 'package:analyzer/src/summary/format.dart'; | 9 import 'package:analyzer/src/summary/format.dart'; |
| 10 import 'package:analyzer/src/summary/idl.dart'; | 10 import 'package:analyzer/src/summary/idl.dart'; |
| 11 | 11 |
| 12 /** | 12 /** |
| 13 * Serialize the given constructor initializer [node]. |
| 14 */ |
| 15 UnlinkedConstructorInitializer serializeConstructorInitializer( |
| 16 ConstructorInitializer node, |
| 17 UnlinkedConstBuilder serializeConstExpr(Expression expr)) { |
| 18 if (node is ConstructorFieldInitializer) { |
| 19 return new UnlinkedConstructorInitializerBuilder( |
| 20 kind: UnlinkedConstructorInitializerKind.field, |
| 21 name: node.fieldName.name, |
| 22 expression: serializeConstExpr(node.expression)); |
| 23 } |
| 24 if (node is RedirectingConstructorInvocation) { |
| 25 return new UnlinkedConstructorInitializerBuilder( |
| 26 kind: UnlinkedConstructorInitializerKind.thisInvocation, |
| 27 name: node?.constructorName?.name, |
| 28 arguments: |
| 29 node.argumentList.arguments.map(serializeConstExpr).toList()); |
| 30 } |
| 31 if (node is SuperConstructorInvocation) { |
| 32 return new UnlinkedConstructorInitializerBuilder( |
| 33 kind: UnlinkedConstructorInitializerKind.superInvocation, |
| 34 name: node?.constructorName?.name, |
| 35 arguments: |
| 36 node.argumentList.arguments.map(serializeConstExpr).toList()); |
| 37 } |
| 38 throw new StateError('Unexpected initializer type ${node.runtimeType}'); |
| 39 } |
| 40 |
| 41 /** |
| 13 * Instances of this class keep track of intermediate state during | 42 * Instances of this class keep track of intermediate state during |
| 14 * serialization of a single constant [Expression]. | 43 * serialization of a single constant [Expression]. |
| 15 */ | 44 */ |
| 16 abstract class AbstractConstExprSerializer { | 45 abstract class AbstractConstExprSerializer { |
| 17 /** | 46 /** |
| 18 * See [UnlinkedConstBuilder.operations]. | 47 * See [UnlinkedConstBuilder.operations]. |
| 19 */ | 48 */ |
| 20 final List<UnlinkedConstOperation> operations = <UnlinkedConstOperation>[]; | 49 final List<UnlinkedConstOperation> operations = <UnlinkedConstOperation>[]; |
| 21 | 50 |
| 22 /** | 51 /** |
| (...skipping 10 matching lines...) Expand all Loading... |
| 33 * See [UnlinkedConstBuilder.strings]. | 62 * See [UnlinkedConstBuilder.strings]. |
| 34 */ | 63 */ |
| 35 final List<String> strings = <String>[]; | 64 final List<String> strings = <String>[]; |
| 36 | 65 |
| 37 /** | 66 /** |
| 38 * See [UnlinkedConstBuilder.references]. | 67 * See [UnlinkedConstBuilder.references]. |
| 39 */ | 68 */ |
| 40 final List<EntityRefBuilder> references = <EntityRefBuilder>[]; | 69 final List<EntityRefBuilder> references = <EntityRefBuilder>[]; |
| 41 | 70 |
| 42 /** | 71 /** |
| 72 * Return `true` if a constructor initializer expression is being serialized |
| 73 * and the given [name] is a constructor parameter reference. |
| 74 */ |
| 75 bool isConstructorParameterName(String name); |
| 76 |
| 77 /** |
| 43 * Serialize the given [expr] expression into this serializer state. | 78 * Serialize the given [expr] expression into this serializer state. |
| 44 */ | 79 */ |
| 45 void serialize(Expression expr) { | 80 void serialize(Expression expr) { |
| 46 if (expr is IntegerLiteral) { | 81 if (expr is IntegerLiteral) { |
| 47 _pushInt(expr.value); | 82 _pushInt(expr.value); |
| 48 } else if (expr is DoubleLiteral) { | 83 } else if (expr is DoubleLiteral) { |
| 49 operations.add(UnlinkedConstOperation.pushDouble); | 84 operations.add(UnlinkedConstOperation.pushDouble); |
| 50 doubles.add(expr.value); | 85 doubles.add(expr.value); |
| 51 } else if (expr is BooleanLiteral) { | 86 } else if (expr is BooleanLiteral) { |
| 52 if (expr.value) { | 87 if (expr.value) { |
| 53 operations.add(UnlinkedConstOperation.pushTrue); | 88 operations.add(UnlinkedConstOperation.pushTrue); |
| 54 } else { | 89 } else { |
| 55 operations.add(UnlinkedConstOperation.pushFalse); | 90 operations.add(UnlinkedConstOperation.pushFalse); |
| 56 } | 91 } |
| 57 } else if (expr is StringLiteral) { | 92 } else if (expr is StringLiteral) { |
| 58 _serializeString(expr); | 93 _serializeString(expr); |
| 59 } else if (expr is SymbolLiteral) { | 94 } else if (expr is SymbolLiteral) { |
| 60 strings.add(expr.components.map((token) => token.lexeme).join('.')); | 95 strings.add(expr.components.map((token) => token.lexeme).join('.')); |
| 61 operations.add(UnlinkedConstOperation.makeSymbol); | 96 operations.add(UnlinkedConstOperation.makeSymbol); |
| 62 } else if (expr is NullLiteral) { | 97 } else if (expr is NullLiteral) { |
| 63 operations.add(UnlinkedConstOperation.pushNull); | 98 operations.add(UnlinkedConstOperation.pushNull); |
| 64 } else if (expr is Identifier) { | 99 } else if (expr is Identifier) { |
| 65 references.add(serializeIdentifier(expr)); | 100 if (expr is SimpleIdentifier && isConstructorParameterName(expr.name)) { |
| 66 operations.add(UnlinkedConstOperation.pushReference); | 101 strings.add(expr.name); |
| 102 operations.add(UnlinkedConstOperation.pushConstructorParameter); |
| 103 } else { |
| 104 references.add(serializeIdentifier(expr)); |
| 105 operations.add(UnlinkedConstOperation.pushReference); |
| 106 } |
| 67 } else if (expr is InstanceCreationExpression) { | 107 } else if (expr is InstanceCreationExpression) { |
| 68 serializeInstanceCreation( | 108 serializeInstanceCreation( |
| 69 serializeConstructorName( | 109 serializeConstructorName( |
| 70 expr.constructorName.type, expr.constructorName.name), | 110 expr.constructorName.type, expr.constructorName.name), |
| 71 expr.argumentList); | 111 expr.argumentList); |
| 72 } else if (expr is ListLiteral) { | 112 } else if (expr is ListLiteral) { |
| 73 _serializeListLiteral(expr); | 113 _serializeListLiteral(expr); |
| 74 } else if (expr is MapLiteral) { | 114 } else if (expr is MapLiteral) { |
| 75 _serializeMapLiteral(expr); | 115 _serializeMapLiteral(expr); |
| 76 } else if (expr is MethodInvocation) { | 116 } else if (expr is MethodInvocation) { |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 * Error that describes a problem during a constant expression serialization. | 357 * Error that describes a problem during a constant expression serialization. |
| 318 */ | 358 */ |
| 319 class _ConstExprSerializationError { | 359 class _ConstExprSerializationError { |
| 320 final String message; | 360 final String message; |
| 321 | 361 |
| 322 _ConstExprSerializationError(this.message); | 362 _ConstExprSerializationError(this.message); |
| 323 | 363 |
| 324 @override | 364 @override |
| 325 String toString() => message; | 365 String toString() => message; |
| 326 } | 366 } |
| OLD | NEW |