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/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/element/type.dart' show DartType; | |
| 9 import 'package:analyzer/src/summary/format.dart'; | 10 import 'package:analyzer/src/summary/format.dart'; |
| 10 import 'package:analyzer/src/summary/idl.dart'; | 11 import 'package:analyzer/src/summary/idl.dart'; |
| 11 | 12 |
| 12 /** | 13 /** |
| 13 * Serialize the given constructor initializer [node]. | 14 * Serialize the given constructor initializer [node]. |
| 14 */ | 15 */ |
| 15 UnlinkedConstructorInitializer serializeConstructorInitializer( | 16 UnlinkedConstructorInitializer serializeConstructorInitializer( |
| 16 ConstructorInitializer node, | 17 ConstructorInitializer node, |
| 17 UnlinkedConstBuilder serializeConstExpr(Expression expr)) { | 18 UnlinkedConstBuilder serializeConstExpr(Expression expr)) { |
| 18 if (node is ConstructorFieldInitializer) { | 19 if (node is ConstructorFieldInitializer) { |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 96 } | 97 } |
| 97 } | 98 } |
| 98 | 99 |
| 99 /** | 100 /** |
| 100 * Serialize the given [annotation] into this serializer state. | 101 * Serialize the given [annotation] into this serializer state. |
| 101 */ | 102 */ |
| 102 void serializeAnnotation(Annotation annotation); | 103 void serializeAnnotation(Annotation annotation); |
| 103 | 104 |
| 104 /** | 105 /** |
| 105 * Return [EntityRefBuilder] that corresponds to the constructor having name | 106 * Return [EntityRefBuilder] that corresponds to the constructor having name |
| 106 * [name] in the class identified by [type]. | 107 * [name] in the class identified by [typeName]. |
|
Paul Berry
2016/05/02 20:35:48
Can you include some documentation of [type] and [
scheglov
2016/05/02 20:55:04
Done.
| |
| 107 */ | 108 */ |
| 108 EntityRefBuilder serializeConstructorName( | 109 EntityRefBuilder serializeConstructorRef(DartType type, Identifier typeName, |
| 109 TypeName type, SimpleIdentifier name); | 110 TypeArgumentList typeArguments, SimpleIdentifier name); |
| 110 | 111 |
| 111 /** | 112 /** |
| 112 * Return [EntityRefBuilder] that corresponds to the given [identifier]. | 113 * Return [EntityRefBuilder] that corresponds to the given [identifier]. |
| 113 */ | 114 */ |
| 114 EntityRefBuilder serializeIdentifier(Identifier identifier); | 115 EntityRefBuilder serializeIdentifier(Identifier identifier); |
| 115 | 116 |
| 116 /** | 117 /** |
| 117 * Return [EntityRefBuilder] that corresponds to the given [expr], which | 118 * Return [EntityRefBuilder] that corresponds to the given [expr], which |
| 118 * must be a sequence of identifiers. | 119 * must be a sequence of identifiers. |
| 119 */ | 120 */ |
| 120 EntityRefBuilder serializeIdentifierSequence(Expression expr); | 121 EntityRefBuilder serializeIdentifierSequence(Expression expr); |
| 121 | 122 |
| 122 void serializeInstanceCreation( | 123 void serializeInstanceCreation( |
| 123 EntityRefBuilder constructor, ArgumentList argumentList) { | 124 EntityRefBuilder constructor, ArgumentList argumentList) { |
| 124 _serializeArguments(argumentList); | 125 _serializeArguments(argumentList); |
| 125 references.add(constructor); | 126 references.add(constructor); |
| 126 operations.add(UnlinkedConstOperation.invokeConstructor); | 127 operations.add(UnlinkedConstOperation.invokeConstructor); |
| 127 } | 128 } |
| 128 | 129 |
| 129 /** | 130 /** |
| 131 * Return [EntityRefBuilder] that corresponds to the [name] with the given | |
|
Paul Berry
2016/05/02 20:35:48
I think you mean "...to the [type] with the given
scheglov
2016/05/02 20:55:04
Acknowledged.
| |
| 132 * [name] and [arguments]. The parameter [type] might be `null` if the | |
| 133 * type is not resolved. | |
| 134 */ | |
| 135 EntityRefBuilder serializeType( | |
| 136 DartType type, Identifier name, TypeArgumentList arguments); | |
| 137 | |
| 138 /** | |
| 130 * Return [EntityRefBuilder] that corresponds to the given [type]. | 139 * Return [EntityRefBuilder] that corresponds to the given [type]. |
| 131 */ | 140 */ |
| 132 EntityRefBuilder serializeType(TypeName type); | 141 EntityRefBuilder serializeTypeName(TypeName type) { |
| 142 return serializeType(type?.type, type?.name, type?.typeArguments); | |
| 143 } | |
| 133 | 144 |
| 134 /** | 145 /** |
| 135 * Return the [UnlinkedConstBuilder] that corresponds to the state of this | 146 * Return the [UnlinkedConstBuilder] that corresponds to the state of this |
| 136 * serializer. | 147 * serializer. |
| 137 */ | 148 */ |
| 138 UnlinkedConstBuilder toBuilder() { | 149 UnlinkedConstBuilder toBuilder() { |
| 139 return new UnlinkedConstBuilder( | 150 return new UnlinkedConstBuilder( |
| 140 isValidConst: isValidConst, | 151 isValidConst: isValidConst, |
| 141 operations: operations, | 152 operations: operations, |
| 142 assignmentOperators: assignmentOperators, | 153 assignmentOperators: assignmentOperators, |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 219 strings.add(expr.name); | 230 strings.add(expr.name); |
| 220 operations.add(UnlinkedConstOperation.pushConstructorParameter); | 231 operations.add(UnlinkedConstOperation.pushConstructorParameter); |
| 221 } else { | 232 } else { |
| 222 references.add(serializeIdentifier(expr)); | 233 references.add(serializeIdentifier(expr)); |
| 223 operations.add(UnlinkedConstOperation.pushReference); | 234 operations.add(UnlinkedConstOperation.pushReference); |
| 224 } | 235 } |
| 225 } else if (expr is InstanceCreationExpression) { | 236 } else if (expr is InstanceCreationExpression) { |
| 226 if (!expr.isConst) { | 237 if (!expr.isConst) { |
| 227 isValidConst = false; | 238 isValidConst = false; |
| 228 } | 239 } |
| 240 TypeName typeName = expr.constructorName.type; | |
| 229 serializeInstanceCreation( | 241 serializeInstanceCreation( |
| 230 serializeConstructorName( | 242 serializeConstructorRef(typeName.type, typeName.name, |
| 231 expr.constructorName.type, expr.constructorName.name), | 243 typeName.typeArguments, expr.constructorName.name), |
| 232 expr.argumentList); | 244 expr.argumentList); |
| 233 } else if (expr is ListLiteral) { | 245 } else if (expr is ListLiteral) { |
| 234 _serializeListLiteral(expr); | 246 _serializeListLiteral(expr); |
| 235 } else if (expr is MapLiteral) { | 247 } else if (expr is MapLiteral) { |
| 236 _serializeMapLiteral(expr); | 248 _serializeMapLiteral(expr); |
| 237 } else if (expr is MethodInvocation) { | 249 } else if (expr is MethodInvocation) { |
| 238 _serializeMethodInvocation(expr); | 250 _serializeMethodInvocation(expr); |
| 239 } else if (expr is BinaryExpression) { | 251 } else if (expr is BinaryExpression) { |
| 240 _serializeBinaryExpression(expr); | 252 _serializeBinaryExpression(expr); |
| 241 } else if (expr is ConditionalExpression) { | 253 } else if (expr is ConditionalExpression) { |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 264 isValidConst = false; | 276 isValidConst = false; |
| 265 // TODO(scheglov) implement | 277 // TODO(scheglov) implement |
| 266 operations.add(UnlinkedConstOperation.pushNull); | 278 operations.add(UnlinkedConstOperation.pushNull); |
| 267 } else if (expr is FunctionExpressionInvocation) { | 279 } else if (expr is FunctionExpressionInvocation) { |
| 268 isValidConst = false; | 280 isValidConst = false; |
| 269 // TODO(scheglov) implement | 281 // TODO(scheglov) implement |
| 270 operations.add(UnlinkedConstOperation.pushNull); | 282 operations.add(UnlinkedConstOperation.pushNull); |
| 271 } else if (expr is AsExpression) { | 283 } else if (expr is AsExpression) { |
| 272 isValidConst = false; | 284 isValidConst = false; |
| 273 _serialize(expr.expression); | 285 _serialize(expr.expression); |
| 274 references.add(serializeType(expr.type)); | 286 references.add(serializeTypeName(expr.type)); |
| 275 operations.add(UnlinkedConstOperation.typeCast); | 287 operations.add(UnlinkedConstOperation.typeCast); |
| 276 } else if (expr is IsExpression) { | 288 } else if (expr is IsExpression) { |
| 277 isValidConst = false; | 289 isValidConst = false; |
| 278 _serialize(expr.expression); | 290 _serialize(expr.expression); |
| 279 references.add(serializeType(expr.type)); | 291 references.add(serializeTypeName(expr.type)); |
| 280 operations.add(UnlinkedConstOperation.typeCheck); | 292 operations.add(UnlinkedConstOperation.typeCheck); |
| 281 } else if (expr is ThrowExpression) { | 293 } else if (expr is ThrowExpression) { |
| 282 isValidConst = false; | 294 isValidConst = false; |
| 283 _serialize(expr.expression); | 295 _serialize(expr.expression); |
| 284 operations.add(UnlinkedConstOperation.throwException); | 296 operations.add(UnlinkedConstOperation.throwException); |
| 285 } else { | 297 } else { |
| 286 throw new StateError('Unknown expression type: $expr'); | 298 throw new StateError('Unknown expression type: $expr'); |
| 287 } | 299 } |
| 288 } | 300 } |
| 289 | 301 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 401 operations.add(UnlinkedConstOperation.cascadeSectionEnd); | 413 operations.add(UnlinkedConstOperation.cascadeSectionEnd); |
| 402 } | 414 } |
| 403 } | 415 } |
| 404 | 416 |
| 405 void _serializeListLiteral(ListLiteral expr) { | 417 void _serializeListLiteral(ListLiteral expr) { |
| 406 List<Expression> elements = expr.elements; | 418 List<Expression> elements = expr.elements; |
| 407 elements.forEach(_serialize); | 419 elements.forEach(_serialize); |
| 408 ints.add(elements.length); | 420 ints.add(elements.length); |
| 409 if (expr.typeArguments != null && | 421 if (expr.typeArguments != null && |
| 410 expr.typeArguments.arguments.length == 1) { | 422 expr.typeArguments.arguments.length == 1) { |
| 411 references.add(serializeType(expr.typeArguments.arguments[0])); | 423 references.add(serializeTypeName(expr.typeArguments.arguments[0])); |
| 412 operations.add(UnlinkedConstOperation.makeTypedList); | 424 operations.add(UnlinkedConstOperation.makeTypedList); |
| 413 } else { | 425 } else { |
| 414 operations.add(UnlinkedConstOperation.makeUntypedList); | 426 operations.add(UnlinkedConstOperation.makeUntypedList); |
| 415 } | 427 } |
| 416 } | 428 } |
| 417 | 429 |
| 418 void _serializeMapLiteral(MapLiteral expr) { | 430 void _serializeMapLiteral(MapLiteral expr) { |
| 419 for (MapLiteralEntry entry in expr.entries) { | 431 for (MapLiteralEntry entry in expr.entries) { |
| 420 _serialize(entry.key); | 432 _serialize(entry.key); |
| 421 _serialize(entry.value); | 433 _serialize(entry.value); |
| 422 } | 434 } |
| 423 ints.add(expr.entries.length); | 435 ints.add(expr.entries.length); |
| 424 if (expr.typeArguments != null && | 436 if (expr.typeArguments != null && |
| 425 expr.typeArguments.arguments.length == 2) { | 437 expr.typeArguments.arguments.length == 2) { |
| 426 references.add(serializeType(expr.typeArguments.arguments[0])); | 438 references.add(serializeTypeName(expr.typeArguments.arguments[0])); |
| 427 references.add(serializeType(expr.typeArguments.arguments[1])); | 439 references.add(serializeTypeName(expr.typeArguments.arguments[1])); |
| 428 operations.add(UnlinkedConstOperation.makeTypedMap); | 440 operations.add(UnlinkedConstOperation.makeTypedMap); |
| 429 } else { | 441 } else { |
| 430 operations.add(UnlinkedConstOperation.makeUntypedMap); | 442 operations.add(UnlinkedConstOperation.makeUntypedMap); |
| 431 } | 443 } |
| 432 } | 444 } |
| 433 | 445 |
| 434 void _serializeMethodInvocation(MethodInvocation invocation) { | 446 void _serializeMethodInvocation(MethodInvocation invocation) { |
| 435 if (invocation.target != null || | 447 if (invocation.target != null || |
| 436 invocation.methodName.name != 'identical') { | 448 invocation.methodName.name != 'identical') { |
| 437 isValidConst = false; | 449 isValidConst = false; |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 556 expr = (expr as PrefixedIdentifier).prefix; | 568 expr = (expr as PrefixedIdentifier).prefix; |
| 557 } else if (expr is PropertyAccess) { | 569 } else if (expr is PropertyAccess) { |
| 558 expr = (expr as PropertyAccess).target; | 570 expr = (expr as PropertyAccess).target; |
| 559 } else { | 571 } else { |
| 560 return false; | 572 return false; |
| 561 } | 573 } |
| 562 } | 574 } |
| 563 return false; | 575 return false; |
| 564 } | 576 } |
| 565 } | 577 } |
| OLD | NEW |