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/ast/token.dart'; | 10 import 'package:analyzer/dart/ast/token.dart'; |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 } | 292 } |
293 return hasLibrarySummary(uri); | 293 return hasLibrarySummary(uri); |
294 } | 294 } |
295 } | 295 } |
296 | 296 |
297 /** | 297 /** |
298 * Builder of [Expression]s from [UnlinkedConst]s. | 298 * Builder of [Expression]s from [UnlinkedConst]s. |
299 */ | 299 */ |
300 class _ConstExprBuilder { | 300 class _ConstExprBuilder { |
301 final _UnitResynthesizer resynthesizer; | 301 final _UnitResynthesizer resynthesizer; |
302 final Element context; | 302 final ElementImpl context; |
303 final UnlinkedConst uc; | 303 final UnlinkedConst uc; |
304 | 304 |
305 bool _typeParameterContextReady = false; | |
306 TypeParameterizedElementMixin _typeParameterContext; | |
307 | |
308 int intPtr = 0; | 305 int intPtr = 0; |
309 int doublePtr = 0; | 306 int doublePtr = 0; |
310 int stringPtr = 0; | 307 int stringPtr = 0; |
311 int refPtr = 0; | 308 int refPtr = 0; |
312 final List<Expression> stack = <Expression>[]; | 309 final List<Expression> stack = <Expression>[]; |
313 | 310 |
314 _ConstExprBuilder(this.resynthesizer, this.context, this.uc); | 311 _ConstExprBuilder(this.resynthesizer, this.context, this.uc); |
315 | 312 |
316 TypeParameterizedElementMixin get typeParameterContext { | |
317 if (!_typeParameterContextReady) { | |
318 for (Element e = context; e != null; e = e.enclosingElement) { | |
319 if (e is TypeParameterizedElementMixin) { | |
320 _typeParameterContext = e; | |
321 break; | |
322 } | |
323 } | |
324 _typeParameterContextReady = true; | |
325 } | |
326 return _typeParameterContext; | |
327 } | |
328 | |
329 Expression build() { | 313 Expression build() { |
330 if (!uc.isValidConst) { | 314 if (!uc.isValidConst) { |
331 return AstFactory.identifier3(r'$$invalidConstExpr$$'); | 315 return AstFactory.identifier3(r'$$invalidConstExpr$$'); |
332 } | 316 } |
333 for (UnlinkedConstOperation operation in uc.operations) { | 317 for (UnlinkedConstOperation operation in uc.operations) { |
334 switch (operation) { | 318 switch (operation) { |
335 case UnlinkedConstOperation.pushNull: | 319 case UnlinkedConstOperation.pushNull: |
336 _push(AstFactory.nullLiteral()); | 320 _push(AstFactory.nullLiteral()); |
337 break; | 321 break; |
338 // bool | 322 // bool |
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
629 if (info.element != null) { | 613 if (info.element != null) { |
630 if (info.element is ConstructorElement) { | 614 if (info.element is ConstructorElement) { |
631 constructorName = info.name; | 615 constructorName = info.name; |
632 } else if (info.element is ClassElement) { | 616 } else if (info.element is ClassElement) { |
633 constructorName = null; | 617 constructorName = null; |
634 } else { | 618 } else { |
635 throw new StateError('Unsupported element for invokeConstructor ' | 619 throw new StateError('Unsupported element for invokeConstructor ' |
636 '${info.element?.runtimeType}'); | 620 '${info.element?.runtimeType}'); |
637 } | 621 } |
638 InterfaceType definingType = resynthesizer._createConstructorDefiningType( | 622 InterfaceType definingType = resynthesizer._createConstructorDefiningType( |
639 typeParameterContext, info, ref.typeArguments); | 623 context?.typeParameterContext, info, ref.typeArguments); |
640 constructorElement = | 624 constructorElement = |
641 resynthesizer._createConstructorElement(definingType, info); | 625 resynthesizer._createConstructorElement(definingType, info); |
642 typeNode = _buildTypeAst(definingType); | 626 typeNode = _buildTypeAst(definingType); |
643 } else { | 627 } else { |
644 if (info.enclosing != null) { | 628 if (info.enclosing != null) { |
645 if (info.enclosing.enclosing != null) { | 629 if (info.enclosing.enclosing != null) { |
646 PrefixedIdentifier typeName = AstFactory.identifier5( | 630 PrefixedIdentifier typeName = AstFactory.identifier5( |
647 info.enclosing.enclosing.name, info.enclosing.name); | 631 info.enclosing.enclosing.name, info.enclosing.name); |
648 typeName.prefix.staticElement = info.enclosing.enclosing.element; | 632 typeName.prefix.staticElement = info.enclosing.enclosing.element; |
649 typeName.identifier.staticElement = info.enclosing.element; | 633 typeName.identifier.staticElement = info.enclosing.element; |
(...skipping 880 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1530 return typeArguments; | 1514 return typeArguments; |
1531 } | 1515 } |
1532 } | 1516 } |
1533 | 1517 |
1534 class _ResynthesizerContext implements ResynthesizerContext { | 1518 class _ResynthesizerContext implements ResynthesizerContext { |
1535 final _UnitResynthesizer _unitResynthesizer; | 1519 final _UnitResynthesizer _unitResynthesizer; |
1536 | 1520 |
1537 _ResynthesizerContext(this._unitResynthesizer); | 1521 _ResynthesizerContext(this._unitResynthesizer); |
1538 | 1522 |
1539 @override | 1523 @override |
1540 ElementAnnotationImpl buildAnnotation(Element context, UnlinkedConst uc) { | 1524 ElementAnnotationImpl buildAnnotation(ElementImpl context, UnlinkedConst uc) { |
1541 return _unitResynthesizer.buildAnnotation(context, uc); | 1525 return _unitResynthesizer.buildAnnotation(context, uc); |
1542 } | 1526 } |
1543 | 1527 |
1544 @override | 1528 @override |
1545 Expression buildExpression(Element context, UnlinkedConst uc) { | 1529 Expression buildExpression(ElementImpl context, UnlinkedConst uc) { |
1546 return _unitResynthesizer._buildConstExpression(context, uc); | 1530 return _unitResynthesizer._buildConstExpression(context, uc); |
1547 } | 1531 } |
1548 | 1532 |
1549 @override | 1533 @override |
1550 UnitExplicitTopLevelAccessors buildTopLevelAccessors() { | 1534 UnitExplicitTopLevelAccessors buildTopLevelAccessors() { |
1551 return _unitResynthesizer.buildUnitExplicitTopLevelAccessors(); | 1535 return _unitResynthesizer.buildUnitExplicitTopLevelAccessors(); |
1552 } | 1536 } |
1553 | 1537 |
1554 @override | 1538 @override |
1555 List<FunctionElementImpl> buildTopLevelFunctions() { | 1539 List<FunctionElementImpl> buildTopLevelFunctions() { |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1690 } | 1674 } |
1691 | 1675 |
1692 SummaryResynthesizer get summaryResynthesizer => | 1676 SummaryResynthesizer get summaryResynthesizer => |
1693 libraryResynthesizer.summaryResynthesizer; | 1677 libraryResynthesizer.summaryResynthesizer; |
1694 | 1678 |
1695 TypeProvider get typeProvider => summaryResynthesizer.typeProvider; | 1679 TypeProvider get typeProvider => summaryResynthesizer.typeProvider; |
1696 | 1680 |
1697 /** | 1681 /** |
1698 * Build [ElementAnnotationImpl] for the given [UnlinkedConst]. | 1682 * Build [ElementAnnotationImpl] for the given [UnlinkedConst]. |
1699 */ | 1683 */ |
1700 ElementAnnotationImpl buildAnnotation(Element context, UnlinkedConst uc) { | 1684 ElementAnnotationImpl buildAnnotation(ElementImpl context, UnlinkedConst uc) { |
1701 ElementAnnotationImpl elementAnnotation = new ElementAnnotationImpl(unit); | 1685 ElementAnnotationImpl elementAnnotation = new ElementAnnotationImpl(unit); |
1702 Expression constExpr = _buildConstExpression(context, uc); | 1686 Expression constExpr = _buildConstExpression(context, uc); |
1703 if (constExpr is Identifier) { | 1687 if (constExpr is Identifier) { |
1704 elementAnnotation.element = constExpr.staticElement; | 1688 elementAnnotation.element = constExpr.staticElement; |
1705 elementAnnotation.annotationAst = AstFactory.annotation(constExpr); | 1689 elementAnnotation.annotationAst = AstFactory.annotation(constExpr); |
1706 } else if (constExpr is InstanceCreationExpression) { | 1690 } else if (constExpr is InstanceCreationExpression) { |
1707 elementAnnotation.element = constExpr.staticElement; | 1691 elementAnnotation.element = constExpr.staticElement; |
1708 Identifier typeName = constExpr.constructorName.type.name; | 1692 Identifier typeName = constExpr.constructorName.type.name; |
1709 SimpleIdentifier constructorName = constExpr.constructorName.name; | 1693 SimpleIdentifier constructorName = constExpr.constructorName.name; |
1710 if (typeName is SimpleIdentifier && constructorName != null) { | 1694 if (typeName is SimpleIdentifier && constructorName != null) { |
(...skipping 1216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2927 } else if (initializer is RedirectingConstructorInvocation) { | 2911 } else if (initializer is RedirectingConstructorInvocation) { |
2928 SimpleIdentifier nameNode = initializer.constructorName; | 2912 SimpleIdentifier nameNode = initializer.constructorName; |
2929 ConstructorElement element = constructors[nameNode?.name ?? '']; | 2913 ConstructorElement element = constructors[nameNode?.name ?? '']; |
2930 initializer.staticElement = element; | 2914 initializer.staticElement = element; |
2931 nameNode?.staticElement = element; | 2915 nameNode?.staticElement = element; |
2932 } | 2916 } |
2933 } | 2917 } |
2934 } | 2918 } |
2935 } | 2919 } |
2936 | 2920 |
2937 Expression _buildConstExpression(Element context, UnlinkedConst uc) { | 2921 Expression _buildConstExpression(ElementImpl context, UnlinkedConst uc) { |
2938 return new _ConstExprBuilder(this, context, uc).build(); | 2922 return new _ConstExprBuilder(this, context, uc).build(); |
2939 } | 2923 } |
2940 | 2924 |
2941 /** | 2925 /** |
2942 * Return the defining type for a [ConstructorElement] by applying | 2926 * Return the defining type for a [ConstructorElement] by applying |
2943 * [typeArgumentRefs] to the given linked [info]. | 2927 * [typeArgumentRefs] to the given linked [info]. |
2944 */ | 2928 */ |
2945 InterfaceType _createConstructorDefiningType( | 2929 InterfaceType _createConstructorDefiningType( |
2946 TypeParameterizedElementMixin typeParameterContext, | 2930 TypeParameterizedElementMixin typeParameterContext, |
2947 _ReferenceInfo info, | 2931 _ReferenceInfo info, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2987 static String _getElementIdentifier(String name, ReferenceKind kind) { | 2971 static String _getElementIdentifier(String name, ReferenceKind kind) { |
2988 if (kind == ReferenceKind.topLevelPropertyAccessor || | 2972 if (kind == ReferenceKind.topLevelPropertyAccessor || |
2989 kind == ReferenceKind.propertyAccessor) { | 2973 kind == ReferenceKind.propertyAccessor) { |
2990 if (!name.endsWith('=')) { | 2974 if (!name.endsWith('=')) { |
2991 return name + '?'; | 2975 return name + '?'; |
2992 } | 2976 } |
2993 } | 2977 } |
2994 return name; | 2978 return name; |
2995 } | 2979 } |
2996 } | 2980 } |
OLD | NEW |