| 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/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'; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 * and the given [name] is a constructor parameter reference. | 78 * and the given [name] is a constructor parameter reference. |
| 79 */ | 79 */ |
| 80 bool isConstructorParameterName(String name); | 80 bool isConstructorParameterName(String name); |
| 81 | 81 |
| 82 /** | 82 /** |
| 83 * Serialize the given [expr] expression into this serializer state. | 83 * Serialize the given [expr] expression into this serializer state. |
| 84 */ | 84 */ |
| 85 void serialize(Expression expr) { | 85 void serialize(Expression expr) { |
| 86 try { | 86 try { |
| 87 _serialize(expr); | 87 _serialize(expr); |
| 88 } on StateError { | 88 } on StateError catch (e, st) { |
| 89 isInvalid = true; | 89 isInvalid = true; |
| 90 } | 90 } |
| 91 } | 91 } |
| 92 | 92 |
| 93 /** | 93 /** |
| 94 * Serialize the given [annotation] into this serializer state. | 94 * Serialize the given [annotation] into this serializer state. |
| 95 */ | 95 */ |
| 96 void serializeAnnotation(Annotation annotation); | 96 void serializeAnnotation(Annotation annotation); |
| 97 | 97 |
| 98 /** | 98 /** |
| 99 * Return [EntityRefBuilder] that corresponds to the constructor having name | 99 * Return [EntityRefBuilder] that corresponds to the constructor having name |
| 100 * [name] in the class identified by [type]. | 100 * [name] in the class identified by [type]. |
| 101 */ | 101 */ |
| 102 EntityRefBuilder serializeConstructorName( | 102 EntityRefBuilder serializeConstructorName( |
| 103 TypeName type, SimpleIdentifier name); | 103 TypeName type, SimpleIdentifier name); |
| 104 | 104 |
| 105 /** | 105 /** |
| 106 * Return [EntityRefBuilder] that corresponds to the given [identifier]. | 106 * Return [EntityRefBuilder] that corresponds to the given [identifier]. |
| 107 */ | 107 */ |
| 108 EntityRefBuilder serializeIdentifier(Identifier identifier); | 108 EntityRefBuilder serializeIdentifier(Identifier identifier); |
| 109 | 109 |
| 110 /** |
| 111 * Return [EntityRefBuilder] that corresponds to the given [expr], which |
| 112 * must be a sequence of identifiers. |
| 113 */ |
| 114 EntityRefBuilder serializeIdentifierSequence(Expression expr); |
| 115 |
| 110 void serializeInstanceCreation( | 116 void serializeInstanceCreation( |
| 111 EntityRefBuilder constructor, ArgumentList argumentList) { | 117 EntityRefBuilder constructor, ArgumentList argumentList) { |
| 112 List<Expression> arguments = argumentList.arguments; | 118 List<Expression> arguments = argumentList.arguments; |
| 113 // Serialize the arguments. | 119 // Serialize the arguments. |
| 114 List<String> argumentNames = <String>[]; | 120 List<String> argumentNames = <String>[]; |
| 115 arguments.forEach((arg) { | 121 arguments.forEach((arg) { |
| 116 if (arg is NamedExpression) { | 122 if (arg is NamedExpression) { |
| 117 argumentNames.add(arg.name.label.name); | 123 argumentNames.add(arg.name.label.name); |
| 118 _serialize(arg.expression); | 124 _serialize(arg.expression); |
| 119 } else { | 125 } else { |
| 120 _serialize(arg); | 126 _serialize(arg); |
| 121 } | 127 } |
| 122 }); | 128 }); |
| 123 // Add the op-code and numbers of named and positional arguments. | 129 // Add the op-code and numbers of named and positional arguments. |
| 124 operations.add(UnlinkedConstOperation.invokeConstructor); | 130 operations.add(UnlinkedConstOperation.invokeConstructor); |
| 125 ints.add(argumentNames.length); | 131 ints.add(argumentNames.length); |
| 126 strings.addAll(argumentNames); | 132 strings.addAll(argumentNames); |
| 127 ints.add(arguments.length - argumentNames.length); | 133 ints.add(arguments.length - argumentNames.length); |
| 128 // Serialize the reference. | 134 // Serialize the reference. |
| 129 references.add(constructor); | 135 references.add(constructor); |
| 130 } | 136 } |
| 131 | 137 |
| 132 /** | 138 /** |
| 133 * Return [EntityRefBuilder] that corresponds to the given [access]. | |
| 134 */ | |
| 135 EntityRefBuilder serializePropertyAccess(PropertyAccess access); | |
| 136 | |
| 137 /** | |
| 138 * Return [EntityRefBuilder] that corresponds to the given [type]. | 139 * Return [EntityRefBuilder] that corresponds to the given [type]. |
| 139 */ | 140 */ |
| 140 EntityRefBuilder serializeType(TypeName type); | 141 EntityRefBuilder serializeType(TypeName type); |
| 141 | 142 |
| 142 /** | 143 /** |
| 143 * Return the [UnlinkedConstBuilder] that corresponds to the state of this | 144 * Return the [UnlinkedConstBuilder] that corresponds to the state of this |
| 144 * serializer. | 145 * serializer. |
| 145 */ | 146 */ |
| 146 UnlinkedConstBuilder toBuilder() { | 147 UnlinkedConstBuilder toBuilder() { |
| 147 if (isInvalid) { | 148 if (isInvalid) { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 } else if (expr is BinaryExpression) { | 231 } else if (expr is BinaryExpression) { |
| 231 _serializeBinaryExpression(expr); | 232 _serializeBinaryExpression(expr); |
| 232 } else if (expr is ConditionalExpression) { | 233 } else if (expr is ConditionalExpression) { |
| 233 _serialize(expr.condition); | 234 _serialize(expr.condition); |
| 234 _serialize(expr.thenExpression); | 235 _serialize(expr.thenExpression); |
| 235 _serialize(expr.elseExpression); | 236 _serialize(expr.elseExpression); |
| 236 operations.add(UnlinkedConstOperation.conditional); | 237 operations.add(UnlinkedConstOperation.conditional); |
| 237 } else if (expr is PrefixExpression) { | 238 } else if (expr is PrefixExpression) { |
| 238 _serializePrefixExpression(expr); | 239 _serializePrefixExpression(expr); |
| 239 } else if (expr is PropertyAccess) { | 240 } else if (expr is PropertyAccess) { |
| 240 if (expr.target is! PrefixedIdentifier && | 241 _serializePropertyAccess(expr); |
| 241 expr.propertyName.name == 'length') { | |
| 242 _serialize(expr.target); | |
| 243 operations.add(UnlinkedConstOperation.length); | |
| 244 } else { | |
| 245 references.add(serializePropertyAccess(expr)); | |
| 246 operations.add(UnlinkedConstOperation.pushReference); | |
| 247 } | |
| 248 } else if (expr is ParenthesizedExpression) { | 242 } else if (expr is ParenthesizedExpression) { |
| 249 _serialize(expr.expression); | 243 _serialize(expr.expression); |
| 250 } else { | 244 } else { |
| 251 throw new StateError('Unknown expression type: $expr'); | 245 throw new StateError('Unknown expression type: $expr'); |
| 252 } | 246 } |
| 253 } | 247 } |
| 254 | 248 |
| 255 void _serializeBinaryExpression(BinaryExpression expr) { | 249 void _serializeBinaryExpression(BinaryExpression expr) { |
| 256 _serialize(expr.leftOperand); | 250 _serialize(expr.leftOperand); |
| 257 _serialize(expr.rightOperand); | 251 _serialize(expr.rightOperand); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 operations.add(UnlinkedConstOperation.not); | 329 operations.add(UnlinkedConstOperation.not); |
| 336 } else if (operator == TokenType.MINUS) { | 330 } else if (operator == TokenType.MINUS) { |
| 337 operations.add(UnlinkedConstOperation.negate); | 331 operations.add(UnlinkedConstOperation.negate); |
| 338 } else if (operator == TokenType.TILDE) { | 332 } else if (operator == TokenType.TILDE) { |
| 339 operations.add(UnlinkedConstOperation.complement); | 333 operations.add(UnlinkedConstOperation.complement); |
| 340 } else { | 334 } else { |
| 341 throw new StateError('Unknown operator: $operator'); | 335 throw new StateError('Unknown operator: $operator'); |
| 342 } | 336 } |
| 343 } | 337 } |
| 344 | 338 |
| 339 void _serializePropertyAccess(PropertyAccess expr) { |
| 340 if (_isIdentifierSequence(expr)) { |
| 341 EntityRefBuilder ref = serializeIdentifierSequence(expr); |
| 342 references.add(ref); |
| 343 operations.add(UnlinkedConstOperation.pushReference); |
| 344 } else { |
| 345 _serialize(expr.target); |
| 346 strings.add(expr.propertyName.name); |
| 347 operations.add(UnlinkedConstOperation.extractProperty); |
| 348 } |
| 349 } |
| 350 |
| 345 void _serializeString(StringLiteral expr) { | 351 void _serializeString(StringLiteral expr) { |
| 346 if (expr is AdjacentStrings) { | 352 if (expr is AdjacentStrings) { |
| 347 if (expr.strings.every((string) => string is SimpleStringLiteral)) { | 353 if (expr.strings.every((string) => string is SimpleStringLiteral)) { |
| 348 operations.add(UnlinkedConstOperation.pushString); | 354 operations.add(UnlinkedConstOperation.pushString); |
| 349 strings.add(expr.stringValue); | 355 strings.add(expr.stringValue); |
| 350 } else { | 356 } else { |
| 351 expr.strings.forEach(_serializeString); | 357 expr.strings.forEach(_serializeString); |
| 352 operations.add(UnlinkedConstOperation.concatenate); | 358 operations.add(UnlinkedConstOperation.concatenate); |
| 353 ints.add(expr.strings.length); | 359 ints.add(expr.strings.length); |
| 354 } | 360 } |
| 355 } else if (expr is SimpleStringLiteral) { | 361 } else if (expr is SimpleStringLiteral) { |
| 356 operations.add(UnlinkedConstOperation.pushString); | 362 operations.add(UnlinkedConstOperation.pushString); |
| 357 strings.add(expr.value); | 363 strings.add(expr.value); |
| 358 } else { | 364 } else { |
| 359 StringInterpolation interpolation = expr as StringInterpolation; | 365 StringInterpolation interpolation = expr as StringInterpolation; |
| 360 for (InterpolationElement element in interpolation.elements) { | 366 for (InterpolationElement element in interpolation.elements) { |
| 361 if (element is InterpolationString) { | 367 if (element is InterpolationString) { |
| 362 operations.add(UnlinkedConstOperation.pushString); | 368 operations.add(UnlinkedConstOperation.pushString); |
| 363 strings.add(element.value); | 369 strings.add(element.value); |
| 364 } else { | 370 } else { |
| 365 _serialize((element as InterpolationExpression).expression); | 371 _serialize((element as InterpolationExpression).expression); |
| 366 } | 372 } |
| 367 } | 373 } |
| 368 operations.add(UnlinkedConstOperation.concatenate); | 374 operations.add(UnlinkedConstOperation.concatenate); |
| 369 ints.add(interpolation.elements.length); | 375 ints.add(interpolation.elements.length); |
| 370 } | 376 } |
| 371 } | 377 } |
| 378 |
| 379 /** |
| 380 * Return `true` if the given [expr] is a sequence of identifiers. |
| 381 */ |
| 382 static bool _isIdentifierSequence(Expression expr) { |
| 383 while (expr != null) { |
| 384 if (expr is SimpleIdentifier) { |
| 385 return true; |
| 386 } |
| 387 if (expr is PrefixedIdentifier) { |
| 388 expr = (expr as PrefixedIdentifier).prefix; |
| 389 } else if (expr is PropertyAccess) { |
| 390 expr = (expr as PropertyAccess).target; |
| 391 } else { |
| 392 return false; |
| 393 } |
| 394 } |
| 395 return false; |
| 396 } |
| 372 } | 397 } |
| OLD | NEW |