Chromium Code Reviews| 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 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 404 break; | 404 break; |
| 405 case UnlinkedConstOperation.makeUntypedMap: | 405 case UnlinkedConstOperation.makeUntypedMap: |
| 406 _pushMap(null); | 406 _pushMap(null); |
| 407 break; | 407 break; |
| 408 case UnlinkedConstOperation.makeTypedMap: | 408 case UnlinkedConstOperation.makeTypedMap: |
| 409 TypeName keyType = _newTypeName(); | 409 TypeName keyType = _newTypeName(); |
| 410 TypeName valueType = _newTypeName(); | 410 TypeName valueType = _newTypeName(); |
| 411 _pushMap(AstFactory.typeArgumentList(<TypeName>[keyType, valueType])); | 411 _pushMap(AstFactory.typeArgumentList(<TypeName>[keyType, valueType])); |
| 412 break; | 412 break; |
| 413 case UnlinkedConstOperation.pushReference: | 413 case UnlinkedConstOperation.pushReference: |
| 414 EntityRef ref = uc.references[refPtr++]; | |
| 415 _ReferenceInfo info = resynthesizer.referenceInfos[ref.reference]; | |
| 416 if (info.type != null) { | |
| 417 Identifier node = _buildTypeIdentifierAst(info.type); | |
| 418 _push(node); | |
| 419 } else { | |
| 420 throw new StateError( | |
| 421 'Unsupported reference ${info.element?.runtimeType}'); | |
| 422 } | |
| 423 break; | |
| 414 case UnlinkedConstOperation.invokeConstructor: | 424 case UnlinkedConstOperation.invokeConstructor: |
| 415 case UnlinkedConstOperation.length: | 425 case UnlinkedConstOperation.length: |
| 416 return AstFactory.nullLiteral(); | 426 return AstFactory.nullLiteral(); |
| 417 // throw new StateError('Unsupported constant operation $operation'); | 427 // throw new StateError('Unsupported constant operation $operation'); |
| 418 } | 428 } |
| 419 } | 429 } |
| 420 return stack.single; | 430 return stack.single; |
| 421 } | 431 } |
| 422 | 432 |
| 423 TypeName _buildTypeAst(DartType type) { | 433 TypeName _buildTypeAst(DartType type) { |
| 424 if (type is DynamicTypeImpl) { | 434 if (type is DynamicTypeImpl) { |
| 425 TypeName node = AstFactory.typeName4('dynamic'); | 435 TypeName node = AstFactory.typeName4('dynamic'); |
| 426 node.type = type; | 436 node.type = type; |
| 427 (node.name as SimpleIdentifier).staticElement = type.element; | 437 (node.name as SimpleIdentifier).staticElement = type.element; |
| 428 return node; | 438 return node; |
| 429 } else if (type is InterfaceType) { | 439 } else if (type is InterfaceType) { |
| 430 List<TypeName> argumentNodes = | 440 List<TypeName> argumentNodes = |
| 431 type.typeArguments.map(_buildTypeAst).toList(); | 441 type.typeArguments.map(_buildTypeAst).toList(); |
| 432 TypeName node = AstFactory.typeName4(type.name, argumentNodes); | 442 TypeName node = AstFactory.typeName4(type.name, argumentNodes); |
| 433 node.type = type; | 443 node.type = type; |
| 434 (node.name as SimpleIdentifier).staticElement = type.element; | 444 (node.name as SimpleIdentifier).staticElement = type.element; |
| 435 return node; | 445 return node; |
| 436 } | 446 } |
| 437 throw new StateError('Unsupported type $type'); | 447 throw new StateError('Unsupported type $type'); |
| 438 } | 448 } |
| 439 | 449 |
| 450 Identifier _buildTypeIdentifierAst(DartType type) { | |
| 451 String name = type.name; | |
| 452 SimpleIdentifier node = AstFactory.identifier3(name); | |
| 453 node.staticElement = type.element; | |
| 454 return node; | |
| 455 } | |
| 456 | |
| 440 InterpolationElement _newInterpolationElement(Expression expr) { | 457 InterpolationElement _newInterpolationElement(Expression expr) { |
| 441 if (expr is SimpleStringLiteral) { | 458 if (expr is SimpleStringLiteral) { |
| 442 return new InterpolationString(expr.literal, expr.value); | 459 return new InterpolationString(expr.literal, expr.value); |
| 443 } else { | 460 } else { |
| 444 return new InterpolationExpression( | 461 return new InterpolationExpression( |
| 445 TokenFactory.tokenFromType(TokenType.STRING_INTERPOLATION_EXPRESSION), | 462 TokenFactory.tokenFromType(TokenType.STRING_INTERPOLATION_EXPRESSION), |
| 446 expr, | 463 expr, |
| 447 TokenFactory.tokenFromType(TokenType.CLOSE_CURLY_BRACKET)); | 464 TokenFactory.tokenFromType(TokenType.CLOSE_CURLY_BRACKET)); |
| 448 } | 465 } |
| 449 } | 466 } |
| (...skipping 813 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1263 } else { | 1280 } else { |
| 1264 return summaryResynthesizer.typeProvider.dynamicType; | 1281 return summaryResynthesizer.typeProvider.dynamicType; |
| 1265 } | 1282 } |
| 1266 } | 1283 } |
| 1267 if (type.paramReference != 0) { | 1284 if (type.paramReference != 0) { |
| 1268 // TODO(paulberry): make this work for generic methods. | 1285 // TODO(paulberry): make this work for generic methods. |
| 1269 return currentTypeParameters[ | 1286 return currentTypeParameters[ |
| 1270 currentTypeParameters.length - type.paramReference] | 1287 currentTypeParameters.length - type.paramReference] |
| 1271 .type; | 1288 .type; |
| 1272 } else { | 1289 } else { |
| 1273 DartType getTypeParameter(int i) { | 1290 DartType getTypeArgument(int i) { |
| 1274 if (i < type.typeArguments.length) { | 1291 if (i < type.typeArguments.length) { |
| 1275 return buildType(type.typeArguments[i]); | 1292 return buildType(type.typeArguments[i]); |
| 1276 } else { | 1293 } else { |
| 1277 return summaryResynthesizer.typeProvider.dynamicType; | 1294 return summaryResynthesizer.typeProvider.dynamicType; |
| 1278 } | 1295 } |
| 1279 } | 1296 } |
| 1280 _ReferenceInfo referenceInfo = referenceInfos[type.reference]; | 1297 _ReferenceInfo referenceInfo = referenceInfos[type.reference]; |
| 1281 return referenceInfo.buildType( | 1298 return referenceInfo.buildType( |
| 1282 getTypeParameter, type.implicitFunctionTypeIndices); | 1299 getTypeArgument, type.implicitFunctionTypeIndices); |
| 1283 } | 1300 } |
| 1284 } | 1301 } |
| 1285 | 1302 |
| 1286 /** | 1303 /** |
| 1287 * Resynthesize a [FunctionTypeAliasElement] and place it in the | 1304 * Resynthesize a [FunctionTypeAliasElement] and place it in the |
| 1288 * [unitHolder]. | 1305 * [unitHolder]. |
| 1289 */ | 1306 */ |
| 1290 void buildTypedef(UnlinkedTypedef serializedTypedef) { | 1307 void buildTypedef(UnlinkedTypedef serializedTypedef) { |
| 1291 try { | 1308 try { |
| 1292 currentTypeParameters = | 1309 currentTypeParameters = |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1584 DartType type; | 1601 DartType type; |
| 1585 | 1602 |
| 1586 /** | 1603 /** |
| 1587 * The number of type parameters accepted by the entity referred to by this | 1604 * The number of type parameters accepted by the entity referred to by this |
| 1588 * reference, or zero if it doesn't accept any type parameters. | 1605 * reference, or zero if it doesn't accept any type parameters. |
| 1589 */ | 1606 */ |
| 1590 final int numTypeParameters; | 1607 final int numTypeParameters; |
| 1591 | 1608 |
| 1592 /** | 1609 /** |
| 1593 * Create a new [_ReferenceInfo] object referring to an element called [name] | 1610 * Create a new [_ReferenceInfo] object referring to an element called [name] |
| 1594 * via the element handle [elementHandle], and having [numTypeParameters] | 1611 * via the element handle [element], and having [numTypeParameters] type |
| 1595 * type parameters. | 1612 * parameters. |
| 1596 * | 1613 * |
| 1597 * For the special types `dynamic` and `void`, [specialType] should point to | 1614 * For the special types `dynamic` and `void`, [specialType] should point to |
| 1598 * the type itself. Otherwise, pass `null` and the type will be computed | 1615 * the type itself. Otherwise, pass `null` and the type will be computed |
| 1599 * when appropriate. | 1616 * when appropriate. |
| 1600 */ | 1617 */ |
| 1601 _ReferenceInfo( | 1618 _ReferenceInfo( |
| 1602 this.name, this.element, DartType specialType, this.numTypeParameters) { | 1619 this.name, this.element, DartType specialType, this.numTypeParameters) { |
| 1603 if (specialType != null) { | 1620 if (specialType != null) { |
| 1604 type = specialType; | 1621 type = specialType; |
| 1605 } else if (numTypeParameters == 0) { | 1622 } else { |
| 1606 // We can precompute the type because it doesn't depend on type | |
| 1607 // parameters. | |
| 1608 type = _buildType(null, null); | 1623 type = _buildType(null, null); |
| 1609 } | 1624 } |
| 1610 } | 1625 } |
| 1611 | 1626 |
| 1612 /** | 1627 /** |
| 1613 * Build a [DartType] corresponding to the result of applying some type | 1628 * Build a [DartType] corresponding to the result of applying some type |
| 1614 * arguments to the entity referred to by this [_ReferenceInfo]. The type | 1629 * arguments to the entity referred to by this [_ReferenceInfo]. The type |
| 1615 * arguments are retrieved by calling [getTypeArgument]. | 1630 * arguments are retrieved by calling [getTypeArgument]. |
| 1616 * | 1631 * |
| 1617 * If [implicitFunctionTypeIndices] is not empty, a [DartType] should be | 1632 * If [implicitFunctionTypeIndices] is not empty, a [DartType] should be |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 1631 if (result == null) { | 1646 if (result == null) { |
| 1632 // TODO(paulberry): figure out how to handle this case (which should | 1647 // TODO(paulberry): figure out how to handle this case (which should |
| 1633 // only occur in the event of erroneous code). | 1648 // only occur in the event of erroneous code). |
| 1634 throw new UnimplementedError(); | 1649 throw new UnimplementedError(); |
| 1635 } | 1650 } |
| 1636 return result; | 1651 return result; |
| 1637 } | 1652 } |
| 1638 | 1653 |
| 1639 /** | 1654 /** |
| 1640 * If this reference refers to a type, build a [DartType] which instantiates | 1655 * If this reference refers to a type, build a [DartType] which instantiates |
| 1641 * it with type arguments returned by [getTypeArgument]. Otherwise return | 1656 * it with type arguments returned by [getTypeArgument] or with all `dynamic` |
| 1642 * `null`. | 1657 * type arguments if [getTypeArgument] is `null`. Otherwise return `null`. |
| 1643 * | 1658 * |
| 1644 * If [implicitFunctionTypeIndices] is not null, a [DartType] should be | 1659 * If [implicitFunctionTypeIndices] is not null, a [DartType] should be |
| 1645 * created which refers to a function type implicitly defined by one of the | 1660 * created which refers to a function type implicitly defined by one of the |
| 1646 * element's parameters. [implicitFunctionTypeIndices] is interpreted as in | 1661 * element's parameters. [implicitFunctionTypeIndices] is interpreted as in |
| 1647 * [EntityRef.implicitFunctionTypeIndices]. | 1662 * [EntityRef.implicitFunctionTypeIndices]. |
| 1648 */ | 1663 */ |
| 1649 DartType _buildType( | 1664 DartType _buildType( |
| 1650 DartType getTypeArgument(int i), List<int> implicitFunctionTypeIndices) { | 1665 DartType getTypeArgument(int i), List<int> implicitFunctionTypeIndices) { |
| 1651 List<DartType> typeArguments = const <DartType>[]; | 1666 List<DartType> typeArguments = const <DartType>[]; |
| 1652 if (numTypeParameters != 0) { | 1667 if (numTypeParameters != 0) { |
| 1653 typeArguments = <DartType>[]; | 1668 typeArguments = <DartType>[]; |
| 1654 for (int i = 0; i < numTypeParameters; i++) { | 1669 for (int i = 0; i < numTypeParameters; i++) { |
| 1655 typeArguments.add(getTypeArgument(i)); | 1670 DartType typeArgument = getTypeArgument != null |
|
Paul Berry
2016/02/01 16:11:40
Nit: Rather than make this change (which will burd
scheglov
2016/02/01 17:22:25
Done.
| |
| 1671 ? getTypeArgument(i) | |
| 1672 : DynamicTypeImpl.instance; | |
| 1673 typeArguments.add(typeArgument); | |
| 1656 } | 1674 } |
| 1657 } | 1675 } |
| 1658 ElementHandle element = this.element; // To allow type promotion | 1676 ElementHandle element = this.element; // To allow type promotion |
| 1659 if (element is ClassElementHandle) { | 1677 if (element is ClassElementHandle) { |
| 1660 return new InterfaceTypeImpl.elementWithNameAndArgs( | 1678 return new InterfaceTypeImpl.elementWithNameAndArgs( |
| 1661 element, name, typeArguments); | 1679 element, name, typeArguments); |
| 1662 } else if (element is FunctionTypeAliasElementHandle) { | 1680 } else if (element is FunctionTypeAliasElementHandle) { |
| 1663 return new FunctionTypeImpl.elementWithNameAndArgs( | 1681 return new FunctionTypeImpl.elementWithNameAndArgs( |
| 1664 element, name, typeArguments, typeArguments.isNotEmpty); | 1682 element, name, typeArguments, typeArguments.isNotEmpty); |
| 1665 } else if (element is FunctionTypedElement && | 1683 } else if (element is FunctionTypedElement && |
| 1666 implicitFunctionTypeIndices != null) { | 1684 implicitFunctionTypeIndices != null) { |
| 1667 FunctionTypedElementComputer computer = () { | 1685 FunctionTypedElementComputer computer = () { |
| 1668 FunctionTypedElement element = this.element; | 1686 FunctionTypedElement element = this.element; |
| 1669 for (int index in implicitFunctionTypeIndices) { | 1687 for (int index in implicitFunctionTypeIndices) { |
| 1670 element = element.parameters[index].type.element; | 1688 element = element.parameters[index].type.element; |
| 1671 } | 1689 } |
| 1672 return element; | 1690 return element; |
| 1673 }; | 1691 }; |
| 1674 // TODO(paulberry): Is it a bug that we have to pass `false` for | 1692 // TODO(paulberry): Is it a bug that we have to pass `false` for |
| 1675 // isInstantiated? | 1693 // isInstantiated? |
| 1676 return new DeferredFunctionTypeImpl(computer, null, typeArguments, false); | 1694 return new DeferredFunctionTypeImpl(computer, null, typeArguments, false); |
| 1677 } else { | 1695 } else { |
| 1678 return null; | 1696 return null; |
| 1679 } | 1697 } |
| 1680 } | 1698 } |
| 1681 } | 1699 } |
| OLD | NEW |