Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(938)

Side by Side Diff: pkg/analyzer/lib/src/dart/element/element.dart

Issue 2989243003: Resynthesize values of final fields of classes with const constructors. (Closed)
Patch Set: Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | pkg/analyzer/lib/src/kernel/resynthesize.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 analyzer.src.dart.element.element; 5 library analyzer.src.dart.element.element;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 import 'dart:math' show min; 8 import 'dart:math' show min;
9 9
10 import 'package:analyzer/dart/ast/ast.dart'; 10 import 'package:analyzer/dart/ast/ast.dart';
(...skipping 1000 matching lines...) Expand 10 before | Expand all | Expand 10 after
1011 ConstructorElement get unnamedConstructor { 1011 ConstructorElement get unnamedConstructor {
1012 for (ConstructorElement element in constructors) { 1012 for (ConstructorElement element in constructors) {
1013 String name = element.displayName; 1013 String name = element.displayName;
1014 if (name == null || name.isEmpty) { 1014 if (name == null || name.isEmpty) {
1015 return element; 1015 return element;
1016 } 1016 }
1017 } 1017 }
1018 return null; 1018 return null;
1019 } 1019 }
1020 1020
1021 bool get _hasConstConstructorKernel =>
1022 _kernel != null && _kernel.constructors.any((c) => c.isConst);
1023
1021 @override 1024 @override
1022 void appendTo(StringBuffer buffer) { 1025 void appendTo(StringBuffer buffer) {
1023 if (isAbstract) { 1026 if (isAbstract) {
1024 buffer.write('abstract '); 1027 buffer.write('abstract ');
1025 } 1028 }
1026 buffer.write('class '); 1029 buffer.write('class ');
1027 String name = displayName; 1030 String name = displayName;
1028 if (name == null) { 1031 if (name == null) {
1029 buffer.write("{unnamed class}"); 1032 buffer.write("{unnamed class}");
1030 } else { 1033 } else {
(...skipping 1557 matching lines...) Expand 10 before | Expand all | Expand 10 after
2588 * in which case there might be some constant variables that lack 2591 * in which case there might be some constant variables that lack
2589 * initializers. 2592 * initializers.
2590 */ 2593 */
2591 Expression _constantInitializer; 2594 Expression _constantInitializer;
2592 2595
2593 EvaluationResultImpl _evaluationResult; 2596 EvaluationResultImpl _evaluationResult;
2594 2597
2595 Expression get constantInitializer { 2598 Expression get constantInitializer {
2596 if (_constantInitializer == null) { 2599 if (_constantInitializer == null) {
2597 if (_kernelInitializer != null) { 2600 if (_kernelInitializer != null) {
2598 _constantInitializer = 2601 _constantInitializer = enclosingUnit._kernelContext
2599 enclosingUnit._kernelContext.getExpression(_kernelInitializer); 2602 .getExpression(this, _kernelInitializer);
2600 } 2603 }
2601 if (_unlinkedConst != null) { 2604 if (_unlinkedConst != null) {
2602 _constantInitializer = enclosingUnit.resynthesizerContext 2605 _constantInitializer = enclosingUnit.resynthesizerContext
2603 .buildExpression(this, _unlinkedConst); 2606 .buildExpression(this, _unlinkedConst);
2604 } 2607 }
2605 } 2608 }
2606 return _constantInitializer; 2609 return _constantInitializer;
2607 } 2610 }
2608 2611
2609 void set constantInitializer(Expression constantInitializer) { 2612 void set constantInitializer(Expression constantInitializer) {
(...skipping 1938 matching lines...) Expand 10 before | Expand all | Expand 10 after
4548 * Initialize using the given kernel. 4551 * Initialize using the given kernel.
4549 */ 4552 */
4550 FieldElementImpl.forKernel(ElementImpl enclosingElement, kernel.Field kernel) 4553 FieldElementImpl.forKernel(ElementImpl enclosingElement, kernel.Field kernel)
4551 : super.forKernel(enclosingElement, kernel); 4554 : super.forKernel(enclosingElement, kernel);
4552 4555
4553 /** 4556 /**
4554 * Initialize using the given kernel. 4557 * Initialize using the given kernel.
4555 */ 4558 */
4556 factory FieldElementImpl.forKernelFactory( 4559 factory FieldElementImpl.forKernelFactory(
4557 ClassElementImpl enclosingClass, kernel.Field kernel) { 4560 ClassElementImpl enclosingClass, kernel.Field kernel) {
4558 if (kernel.isConst) { 4561 if (kernel.isConst ||
4562 kernel.isFinal && enclosingClass._hasConstConstructorKernel) {
4559 return new ConstFieldElementImpl.forKernel(enclosingClass, kernel); 4563 return new ConstFieldElementImpl.forKernel(enclosingClass, kernel);
4560 } else { 4564 } else {
4561 return new FieldElementImpl.forKernel(enclosingClass, kernel); 4565 return new FieldElementImpl.forKernel(enclosingClass, kernel);
4562 } 4566 }
4563 } 4567 }
4564 4568
4565 /** 4569 /**
4566 * Initialize a newly created field element to have the given [name]. 4570 * Initialize a newly created field element to have the given [name].
4567 */ 4571 */
4568 FieldElementImpl.forNode(Identifier name) : super.forNode(name); 4572 FieldElementImpl.forNode(Identifier name) : super.forNode(name);
(...skipping 1537 matching lines...) Expand 10 before | Expand all | Expand 10 after
6106 /** 6110 /**
6107 * Return the resynthesized [ConstructorInitializer] for the given Kernel 6111 * Return the resynthesized [ConstructorInitializer] for the given Kernel
6108 * [initializer], or `null` if synthetic. 6112 * [initializer], or `null` if synthetic.
6109 */ 6113 */
6110 ConstructorInitializer getConstructorInitializer( 6114 ConstructorInitializer getConstructorInitializer(
6111 ConstructorElementImpl constructor, kernel.Initializer initializer); 6115 ConstructorElementImpl constructor, kernel.Initializer initializer);
6112 6116
6113 /** 6117 /**
6114 * Return the [Expression] for the given kernel. 6118 * Return the [Expression] for the given kernel.
6115 */ 6119 */
6116 Expression getExpression(kernel.Expression expression); 6120 Expression getExpression(ElementImpl context, kernel.Expression expression);
6117 6121
6118 /** 6122 /**
6119 * Return the list with exactly two elements - positional and named parameter 6123 * Return the list with exactly two elements - positional and named parameter
6120 * lists. 6124 * lists.
6121 */ 6125 */
6122 List<List<kernel.VariableDeclaration>> getFunctionTypeParameters( 6126 List<List<kernel.VariableDeclaration>> getFunctionTypeParameters(
6123 kernel.FunctionType functionType); 6127 kernel.FunctionType functionType);
6124 6128
6125 /** 6129 /**
6126 * Return the [InterfaceType] for the given Kernel [type], or `null` if the 6130 * Return the [InterfaceType] for the given Kernel [type], or `null` if the
(...skipping 3793 matching lines...) Expand 10 before | Expand all | Expand 10 after
9920 9924
9921 @override 9925 @override
9922 DartObject computeConstantValue() => null; 9926 DartObject computeConstantValue() => null;
9923 9927
9924 @override 9928 @override
9925 void visitChildren(ElementVisitor visitor) { 9929 void visitChildren(ElementVisitor visitor) {
9926 super.visitChildren(visitor); 9930 super.visitChildren(visitor);
9927 _initializer?.accept(visitor); 9931 _initializer?.accept(visitor);
9928 } 9932 }
9929 } 9933 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/lib/src/kernel/resynthesize.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698