| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 summary_resynthesizer; | 5 library summary_resynthesizer; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:analyzer/dart/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
| 10 import 'package:analyzer/dart/element/element.dart'; | 10 import 'package:analyzer/dart/element/element.dart'; |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 return true; | 242 return true; |
| 243 } | 243 } |
| 244 return hasLibrarySummary(uri); | 244 return hasLibrarySummary(uri); |
| 245 } | 245 } |
| 246 } | 246 } |
| 247 | 247 |
| 248 /** | 248 /** |
| 249 * Builder of [Expression]s from [UnlinkedConst]s. | 249 * Builder of [Expression]s from [UnlinkedConst]s. |
| 250 */ | 250 */ |
| 251 class _ConstExprBuilder { | 251 class _ConstExprBuilder { |
| 252 final SummaryResynthesizer resynthesizer; | 252 final _LibraryResynthesizer resynthesizer; |
| 253 final UnlinkedConst uc; | 253 final UnlinkedConst uc; |
| 254 | 254 |
| 255 int intPtr = 0; | 255 int intPtr = 0; |
| 256 int doublePtr = 0; | 256 int doublePtr = 0; |
| 257 int stringPtr = 0; | 257 int stringPtr = 0; |
| 258 int refPtr = 0; | 258 int refPtr = 0; |
| 259 final List<Expression> stack = <Expression>[]; | 259 final List<Expression> stack = <Expression>[]; |
| 260 | 260 |
| 261 _ConstExprBuilder(this.resynthesizer, this.uc); | 261 _ConstExprBuilder(this.resynthesizer, this.uc); |
| 262 | 262 |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 AstFactory.conditionalExpression(condition, thenExpr, elseExpr)); | 387 AstFactory.conditionalExpression(condition, thenExpr, elseExpr)); |
| 388 break; | 388 break; |
| 389 // identical | 389 // identical |
| 390 case UnlinkedConstOperation.identical: | 390 case UnlinkedConstOperation.identical: |
| 391 Expression second = _pop(); | 391 Expression second = _pop(); |
| 392 Expression first = _pop(); | 392 Expression first = _pop(); |
| 393 _push(AstFactory.methodInvocation( | 393 _push(AstFactory.methodInvocation( |
| 394 null, 'identical', <Expression>[first, second])); | 394 null, 'identical', <Expression>[first, second])); |
| 395 break; | 395 break; |
| 396 case UnlinkedConstOperation.makeUntypedList: | 396 case UnlinkedConstOperation.makeUntypedList: |
| 397 int count = uc.ints[intPtr++]; | 397 _pushList(null); |
| 398 List<Expression> elements = <Expression>[]; | 398 break; |
| 399 for (int i= 0; i < count; i++) { | 399 case UnlinkedConstOperation.makeTypedList: |
| 400 elements.insert(0, _pop()); | 400 TypeName itemType = _newTypeName(); |
| 401 } | 401 _pushList(AstFactory.typeArgumentList(<TypeName>[itemType])); |
| 402 _push(AstFactory.listLiteral2(Keyword.CONST, null, elements)); | |
| 403 break; | 402 break; |
| 404 case UnlinkedConstOperation.makeUntypedMap: | 403 case UnlinkedConstOperation.makeUntypedMap: |
| 405 int count = uc.ints[intPtr++]; | 404 _pushMap(null); |
| 406 List<MapLiteralEntry> entries = <MapLiteralEntry>[]; | |
| 407 for (int i= 0; i < count; i++) { | |
| 408 Expression value = _pop(); | |
| 409 Expression key = _pop(); | |
| 410 entries.insert(0, AstFactory.mapLiteralEntry2(key, value)); | |
| 411 } | |
| 412 _push(AstFactory.mapLiteral(Keyword.CONST, null, entries)); | |
| 413 break; | 405 break; |
| 414 case UnlinkedConstOperation.makeTypedList: | |
| 415 case UnlinkedConstOperation.makeTypedMap: | 406 case UnlinkedConstOperation.makeTypedMap: |
| 407 TypeName keyType = _newTypeName(); |
| 408 TypeName valueType = _newTypeName(); |
| 409 _pushMap(AstFactory.typeArgumentList(<TypeName>[keyType, valueType])); |
| 410 break; |
| 416 case UnlinkedConstOperation.pushReference: | 411 case UnlinkedConstOperation.pushReference: |
| 417 case UnlinkedConstOperation.invokeConstructor: | 412 case UnlinkedConstOperation.invokeConstructor: |
| 418 case UnlinkedConstOperation.length: | 413 case UnlinkedConstOperation.length: |
| 419 return AstFactory.nullLiteral(); | 414 return AstFactory.nullLiteral(); |
| 420 // throw new StateError('Unsupported constant operation $operation'); | 415 // throw new StateError('Unsupported constant operation $operation'); |
| 421 } | 416 } |
| 422 } | 417 } |
| 423 return stack.single; | 418 return stack.single; |
| 424 } | 419 } |
| 425 | 420 |
| 421 void _pushMap(TypeArgumentList typeArguments) { |
| 422 int count = uc.ints[intPtr++]; |
| 423 List<MapLiteralEntry> entries = <MapLiteralEntry>[]; |
| 424 for (int i = 0; i < count; i++) { |
| 425 Expression value = _pop(); |
| 426 Expression key = _pop(); |
| 427 entries.insert(0, AstFactory.mapLiteralEntry2(key, value)); |
| 428 } |
| 429 _push(AstFactory.mapLiteral(Keyword.CONST, typeArguments, entries)); |
| 430 } |
| 431 |
| 432 TypeName _buildTypeAst(DartType type) { |
| 433 if (type is DynamicTypeImpl) { |
| 434 return AstFactory.typeName4('dynamic')..type = type; |
| 435 } else if (type is InterfaceType) { |
| 436 List<TypeName> argumentNodes = |
| 437 type.typeArguments.map(_buildTypeAst).toList(); |
| 438 TypeName node = AstFactory.typeName4(type.name, argumentNodes); |
| 439 node.type = type; |
| 440 return node; |
| 441 } |
| 442 throw new StateError('Unsupported type $type'); |
| 443 } |
| 444 |
| 426 InterpolationElement _newInterpolationElement(Expression expr) { | 445 InterpolationElement _newInterpolationElement(Expression expr) { |
| 427 if (expr is SimpleStringLiteral) { | 446 if (expr is SimpleStringLiteral) { |
| 428 return new InterpolationString(expr.literal, expr.value); | 447 return new InterpolationString(expr.literal, expr.value); |
| 429 } else { | 448 } else { |
| 430 return new InterpolationExpression( | 449 return new InterpolationExpression( |
| 431 TokenFactory.tokenFromType(TokenType.STRING_INTERPOLATION_EXPRESSION), | 450 TokenFactory.tokenFromType(TokenType.STRING_INTERPOLATION_EXPRESSION), |
| 432 expr, | 451 expr, |
| 433 TokenFactory.tokenFromType(TokenType.CLOSE_CURLY_BRACKET)); | 452 TokenFactory.tokenFromType(TokenType.CLOSE_CURLY_BRACKET)); |
| 434 } | 453 } |
| 435 } | 454 } |
| 436 | 455 |
| 456 /** |
| 457 * Convert the next reference to the [DartType] and return the AST |
| 458 * corresponding to this type. |
| 459 */ |
| 460 TypeName _newTypeName() { |
| 461 EntityRef typeRef = uc.references[refPtr++]; |
| 462 DartType type = resynthesizer.buildType(typeRef); |
| 463 return _buildTypeAst(type); |
| 464 } |
| 465 |
| 437 Expression _pop() => stack.removeLast(); | 466 Expression _pop() => stack.removeLast(); |
| 438 | 467 |
| 439 void _push(Expression expr) { | 468 void _push(Expression expr) { |
| 440 stack.add(expr); | 469 stack.add(expr); |
| 441 } | 470 } |
| 442 | 471 |
| 443 void _pushBinary(TokenType operator) { | 472 void _pushBinary(TokenType operator) { |
| 444 Expression right = _pop(); | 473 Expression right = _pop(); |
| 445 Expression left = _pop(); | 474 Expression left = _pop(); |
| 446 _push(AstFactory.binaryExpression(left, operator, right)); | 475 _push(AstFactory.binaryExpression(left, operator, right)); |
| 447 } | 476 } |
| 448 | 477 |
| 478 void _pushList(TypeArgumentList typeArguments) { |
| 479 int count = uc.ints[intPtr++]; |
| 480 List<Expression> elements = <Expression>[]; |
| 481 for (int i = 0; i < count; i++) { |
| 482 elements.insert(0, _pop()); |
| 483 } |
| 484 _push(AstFactory.listLiteral2(Keyword.CONST, typeArguments, elements)); |
| 485 } |
| 486 |
| 449 void _pushPrefix(TokenType operator) { | 487 void _pushPrefix(TokenType operator) { |
| 450 Expression operand = _pop(); | 488 Expression operand = _pop(); |
| 451 _push(AstFactory.prefixExpression(operator, operand)); | 489 _push(AstFactory.prefixExpression(operator, operand)); |
| 452 } | 490 } |
| 453 } | 491 } |
| 454 | 492 |
| 455 /** | 493 /** |
| 456 * A single constant variable for which the constant value should be computed. | 494 * A single constant variable for which the constant value should be computed. |
| 457 * | 495 * |
| 458 * TODO(scheglov) we will probably need to add dependency list | 496 * TODO(scheglov) we will probably need to add dependency list |
| (...skipping 859 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1318 void buildVariable(UnlinkedVariable serializedVariable, | 1356 void buildVariable(UnlinkedVariable serializedVariable, |
| 1319 [ElementHolder holder]) { | 1357 [ElementHolder holder]) { |
| 1320 if (holder == null) { | 1358 if (holder == null) { |
| 1321 TopLevelVariableElementImpl element; | 1359 TopLevelVariableElementImpl element; |
| 1322 if (serializedVariable.constExpr != null) { | 1360 if (serializedVariable.constExpr != null) { |
| 1323 ConstTopLevelVariableElementImpl constElement = | 1361 ConstTopLevelVariableElementImpl constElement = |
| 1324 new ConstTopLevelVariableElementImpl( | 1362 new ConstTopLevelVariableElementImpl( |
| 1325 serializedVariable.name, serializedVariable.nameOffset); | 1363 serializedVariable.name, serializedVariable.nameOffset); |
| 1326 element = constElement; | 1364 element = constElement; |
| 1327 // TODO(scheglov) share const builder? | 1365 // TODO(scheglov) share const builder? |
| 1328 _ConstExprBuilder builder = new _ConstExprBuilder( | 1366 _ConstExprBuilder builder = |
| 1329 summaryResynthesizer, serializedVariable.constExpr); | 1367 new _ConstExprBuilder(this, serializedVariable.constExpr); |
| 1330 constElement.constantInitializer = builder.build(); | 1368 constElement.constantInitializer = builder.build(); |
| 1331 constVariables.add(new _ConstVariable(constElement)); | 1369 constVariables.add(new _ConstVariable(constElement)); |
| 1332 } else { | 1370 } else { |
| 1333 element = new TopLevelVariableElementImpl( | 1371 element = new TopLevelVariableElementImpl( |
| 1334 serializedVariable.name, serializedVariable.nameOffset); | 1372 serializedVariable.name, serializedVariable.nameOffset); |
| 1335 } | 1373 } |
| 1336 buildVariableCommonParts(element, serializedVariable); | 1374 buildVariableCommonParts(element, serializedVariable); |
| 1337 unitHolder.addTopLevelVariable(element); | 1375 unitHolder.addTopLevelVariable(element); |
| 1338 buildImplicitAccessors(element, unitHolder); | 1376 buildImplicitAccessors(element, unitHolder); |
| 1339 } else { | 1377 } else { |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1605 return new InterfaceTypeImpl.elementWithNameAndArgs( | 1643 return new InterfaceTypeImpl.elementWithNameAndArgs( |
| 1606 element, name, typeArguments); | 1644 element, name, typeArguments); |
| 1607 } else if (element is FunctionTypeAliasElementHandle) { | 1645 } else if (element is FunctionTypeAliasElementHandle) { |
| 1608 return new FunctionTypeImpl.elementWithNameAndArgs( | 1646 return new FunctionTypeImpl.elementWithNameAndArgs( |
| 1609 element, name, typeArguments, typeArguments.isNotEmpty); | 1647 element, name, typeArguments, typeArguments.isNotEmpty); |
| 1610 } else { | 1648 } else { |
| 1611 return null; | 1649 return null; |
| 1612 } | 1650 } |
| 1613 } | 1651 } |
| 1614 } | 1652 } |
| OLD | NEW |