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_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(expr); | 108 _serializeInstanceCreation(expr); |
| 69 } else if (expr is ListLiteral) { | 109 } else if (expr is ListLiteral) { |
| 70 _serializeListLiteral(expr); | 110 _serializeListLiteral(expr); |
| 71 } else if (expr is MapLiteral) { | 111 } else if (expr is MapLiteral) { |
| 72 _serializeMapLiteral(expr); | 112 _serializeMapLiteral(expr); |
| 73 } else if (expr is MethodInvocation) { | 113 } else if (expr is MethodInvocation) { |
| 74 String name = expr.methodName.name; | 114 String name = expr.methodName.name; |
| 75 if (name != 'identical') { | 115 // if (name != 'identical') { |
|
Paul Berry
2016/02/08 14:53:12
Can you include a comment explaining why this code
scheglov
2016/02/08 16:29:51
It should not be commented out.
I will uncomment i
| |
| 76 throw new _ConstExprSerializationError( | 116 // throw new _ConstExprSerializationError( |
| 77 'Only "identity" function invocation is allowed.'); | 117 // 'Only "identity" function invocation is allowed.'); |
| 78 } | 118 // } |
| 79 if (expr.argumentList == null || | 119 // if (expr.argumentList == null || |
| 80 expr.argumentList.arguments.length != 2) { | 120 // expr.argumentList.arguments.length != 2) { |
| 81 throw new _ConstExprSerializationError( | 121 // throw new _ConstExprSerializationError( |
| 82 'The function "identity" requires exactly 2 arguments.'); | 122 // 'The function "identity" requires exactly 2 arguments.'); |
| 83 } | 123 // } |
| 84 expr.argumentList.arguments.forEach(serialize); | 124 expr.argumentList.arguments.forEach(serialize); |
| 85 operations.add(UnlinkedConstOperation.identical); | 125 operations.add(UnlinkedConstOperation.identical); |
| 86 } else if (expr is BinaryExpression) { | 126 } else if (expr is BinaryExpression) { |
| 87 _serializeBinaryExpression(expr); | 127 _serializeBinaryExpression(expr); |
| 88 } else if (expr is ConditionalExpression) { | 128 } else if (expr is ConditionalExpression) { |
| 89 serialize(expr.condition); | 129 serialize(expr.condition); |
| 90 serialize(expr.thenExpression); | 130 serialize(expr.thenExpression); |
| 91 serialize(expr.elseExpression); | 131 serialize(expr.elseExpression); |
| 92 operations.add(UnlinkedConstOperation.conditional); | 132 operations.add(UnlinkedConstOperation.conditional); |
| 93 } else if (expr is PrefixExpression) { | 133 } else if (expr is PrefixExpression) { |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 307 * Error that describes a problem during a constant expression serialization. | 347 * Error that describes a problem during a constant expression serialization. |
| 308 */ | 348 */ |
| 309 class _ConstExprSerializationError { | 349 class _ConstExprSerializationError { |
| 310 final String message; | 350 final String message; |
| 311 | 351 |
| 312 _ConstExprSerializationError(this.message); | 352 _ConstExprSerializationError(this.message); |
| 313 | 353 |
| 314 @override | 354 @override |
| 315 String toString() => message; | 355 String toString() => message; |
| 316 } | 356 } |
| OLD | NEW |