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

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

Issue 2978113002: Resynthesize literal expressions from Kernel. (Closed)
Patch Set: Created 3 years, 5 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/test/src/summary/resynthesize_kernel_test.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 1595 matching lines...) Expand 10 before | Expand all | Expand 10 after
1606 return _metadata ??= _buildAnnotations( 1606 return _metadata ??= _buildAnnotations(
1607 library.definingCompilationUnit as CompilationUnitElementImpl, 1607 library.definingCompilationUnit as CompilationUnitElementImpl,
1608 _unlinkedPart.annotations); 1608 _unlinkedPart.annotations);
1609 } 1609 }
1610 return super.metadata; 1610 return super.metadata;
1611 } 1611 }
1612 1612
1613 @override 1613 @override
1614 List<TopLevelVariableElement> get topLevelVariables { 1614 List<TopLevelVariableElement> get topLevelVariables {
1615 if (_kernelContext != null) { 1615 if (_kernelContext != null) {
1616 return _variables ??= _kernelContext.library.fields 1616 return _variables ??= _kernelContext.library.fields.map((k) {
1617 .map((k) => new TopLevelVariableElementImpl.forKernel(this, k)) 1617 if (k.isConst && k.initializer != null) {
1618 .toList(growable: false); 1618 return new ConstTopLevelVariableElementImpl.forKernel(this, k);
1619 } else {
1620 return new TopLevelVariableElementImpl.forKernel(this, k);
1621 }
1622 }).toList(growable: false);
1619 } 1623 }
1620 if (_unlinkedUnit != null) { 1624 if (_unlinkedUnit != null) {
1621 if (_variables == null) { 1625 if (_variables == null) {
1622 _explicitTopLevelAccessors ??= 1626 _explicitTopLevelAccessors ??=
1623 resynthesizerContext.buildTopLevelAccessors(); 1627 resynthesizerContext.buildTopLevelAccessors();
1624 _explicitTopLevelVariables ??= 1628 _explicitTopLevelVariables ??=
1625 resynthesizerContext.buildTopLevelVariables(); 1629 resynthesizerContext.buildTopLevelVariables();
1626 List<TopLevelVariableElementImpl> variables = 1630 List<TopLevelVariableElementImpl> variables =
1627 <TopLevelVariableElementImpl>[]; 1631 <TopLevelVariableElementImpl>[];
1628 variables.addAll(_explicitTopLevelVariables.variables); 1632 variables.addAll(_explicitTopLevelVariables.variables);
(...skipping 745 matching lines...) Expand 10 before | Expand all | Expand 10 after
2374 class ConstTopLevelVariableElementImpl extends TopLevelVariableElementImpl 2378 class ConstTopLevelVariableElementImpl extends TopLevelVariableElementImpl
2375 with ConstVariableElement { 2379 with ConstVariableElement {
2376 /** 2380 /**
2377 * Initialize a newly created synthetic top-level variable element to have the 2381 * Initialize a newly created synthetic top-level variable element to have the
2378 * given [name] and [offset]. 2382 * given [name] and [offset].
2379 */ 2383 */
2380 ConstTopLevelVariableElementImpl(String name, int offset) 2384 ConstTopLevelVariableElementImpl(String name, int offset)
2381 : super(name, offset); 2385 : super(name, offset);
2382 2386
2383 /** 2387 /**
2388 * Initialize using the given kernel.
2389 */
2390 ConstTopLevelVariableElementImpl.forKernel(
2391 ElementImpl enclosingElement, kernel.Field kernel)
2392 : super.forKernel(enclosingElement, kernel);
2393
2394 /**
2384 * Initialize a newly created top-level variable element to have the given 2395 * Initialize a newly created top-level variable element to have the given
2385 * [name]. 2396 * [name].
2386 */ 2397 */
2387 ConstTopLevelVariableElementImpl.forNode(Identifier name) 2398 ConstTopLevelVariableElementImpl.forNode(Identifier name)
2388 : super.forNode(name); 2399 : super.forNode(name);
2389 2400
2390 /** 2401 /**
2391 * Initialize using the given serialized information. 2402 * Initialize using the given serialized information.
2392 */ 2403 */
2393 ConstTopLevelVariableElementImpl.forSerialized( 2404 ConstTopLevelVariableElementImpl.forSerialized(
(...skipping 21 matching lines...) Expand all
2415 * Note that in correct Dart code, all constant variables must have 2426 * Note that in correct Dart code, all constant variables must have
2416 * initializers. However, analyzer also needs to handle incorrect Dart code, 2427 * initializers. However, analyzer also needs to handle incorrect Dart code,
2417 * in which case there might be some constant variables that lack 2428 * in which case there might be some constant variables that lack
2418 * initializers. 2429 * initializers.
2419 */ 2430 */
2420 Expression _constantInitializer; 2431 Expression _constantInitializer;
2421 2432
2422 EvaluationResultImpl _evaluationResult; 2433 EvaluationResultImpl _evaluationResult;
2423 2434
2424 Expression get constantInitializer { 2435 Expression get constantInitializer {
2425 if (_constantInitializer == null && _unlinkedConst != null) { 2436 if (_constantInitializer == null) {
2426 _constantInitializer = enclosingUnit.resynthesizerContext 2437 if (_kernelInitializer != null) {
2427 .buildExpression(this, _unlinkedConst); 2438 _constantInitializer =
2439 enclosingUnit._kernelContext.getExpression(_kernelInitializer);
2440 }
2441 if (_unlinkedConst != null) {
2442 _constantInitializer = enclosingUnit.resynthesizerContext
2443 .buildExpression(this, _unlinkedConst);
2444 }
2428 } 2445 }
2429 return _constantInitializer; 2446 return _constantInitializer;
2430 } 2447 }
2431 2448
2432 void set constantInitializer(Expression constantInitializer) { 2449 void set constantInitializer(Expression constantInitializer) {
2433 _assertNotResynthesized(_unlinkedConst); 2450 _assertNotResynthesized(_unlinkedConst);
2434 _constantInitializer = constantInitializer; 2451 _constantInitializer = constantInitializer;
2435 } 2452 }
2436 2453
2437 EvaluationResultImpl get evaluationResult => _evaluationResult; 2454 EvaluationResultImpl get evaluationResult => _evaluationResult;
2438 2455
2439 void set evaluationResult(EvaluationResultImpl evaluationResult) { 2456 void set evaluationResult(EvaluationResultImpl evaluationResult) {
2440 _evaluationResult = evaluationResult; 2457 _evaluationResult = evaluationResult;
2441 } 2458 }
2442 2459
2443 /** 2460 /**
2461 * If this element is resynthesized from Kernel, return the Kernel
2462 * initializer, otherwise return `null`.
2463 */
2464 kernel.Expression get _kernelInitializer;
2465
2466 /**
2444 * If this element is resynthesized from the summary, return the unlinked 2467 * If this element is resynthesized from the summary, return the unlinked
2445 * initializer, otherwise return `null`. 2468 * initializer, otherwise return `null`.
2446 */ 2469 */
2447 UnlinkedExpr get _unlinkedConst; 2470 UnlinkedExpr get _unlinkedConst;
2448 2471
2449 /** 2472 /**
2450 * Return a representation of the value of this variable, forcing the value 2473 * Return a representation of the value of this variable, forcing the value
2451 * to be computed if it had not previously been computed, or `null` if either 2474 * to be computed if it had not previously been computed, or `null` if either
2452 * this variable was not declared with the 'const' modifier or if the value of 2475 * this variable was not declared with the 'const' modifier or if the value of
2453 * this variable could not be computed because of errors. 2476 * this variable could not be computed because of errors.
(...skipping 3207 matching lines...) Expand 10 before | Expand all | Expand 10 after
5661 } 5684 }
5662 } 5685 }
5663 5686
5664 /** 5687 /**
5665 * The kernel context in which a library is resynthesized. 5688 * The kernel context in which a library is resynthesized.
5666 */ 5689 */
5667 abstract class KernelLibraryResynthesizerContext { 5690 abstract class KernelLibraryResynthesizerContext {
5668 kernel.Library get library; 5691 kernel.Library get library;
5669 5692
5670 /** 5693 /**
5694 * Return the [Expression] for the given kernel.
5695 */
5696 Expression getExpression(kernel.Expression expression);
5697
5698 /**
5671 * Return the [InterfaceType] for the given Kernel [type], or `null` if the 5699 * Return the [InterfaceType] for the given Kernel [type], or `null` if the
5672 * [type] does not correspond to an [InterfaceType]. 5700 * [type] does not correspond to an [InterfaceType].
5673 */ 5701 */
5674 InterfaceType getInterfaceType(ElementImpl context, kernel.Supertype type); 5702 InterfaceType getInterfaceType(ElementImpl context, kernel.Supertype type);
5675 5703
5676 /** 5704 /**
5677 * Return the [DartType] for the given Kernel [type], or `null` if the [type] 5705 * Return the [DartType] for the given Kernel [type], or `null` if the [type]
5678 * does not correspond to a [DartType]. 5706 * does not correspond to a [DartType].
5679 */ 5707 */
5680 DartType getType(ElementImpl context, kernel.DartType type); 5708 DartType getType(ElementImpl context, kernel.DartType type);
(...skipping 1693 matching lines...) Expand 10 before | Expand all | Expand 10 after
7374 TopLevelInferenceError get typeInferenceError { 7402 TopLevelInferenceError get typeInferenceError {
7375 if (_unlinkedVariable != null) { 7403 if (_unlinkedVariable != null) {
7376 return enclosingUnit.resynthesizerContext 7404 return enclosingUnit.resynthesizerContext
7377 .getTypeInferenceError(_unlinkedVariable.inferredTypeSlot); 7405 .getTypeInferenceError(_unlinkedVariable.inferredTypeSlot);
7378 } 7406 }
7379 // We don't support type inference errors without linking. 7407 // We don't support type inference errors without linking.
7380 return null; 7408 return null;
7381 } 7409 }
7382 7410
7383 /** 7411 /**
7412 * Subclasses need this getter, see [ConstVariableElement._kernelInitializer].
7413 */
7414 kernel.Expression get _kernelInitializer => _kernel?.initializer;
7415
7416 /**
7384 * Subclasses need this getter, see [ConstVariableElement._unlinkedConst]. 7417 * Subclasses need this getter, see [ConstVariableElement._unlinkedConst].
7385 */ 7418 */
7386 UnlinkedExpr get _unlinkedConst => _unlinkedVariable?.initializer?.bodyExpr; 7419 UnlinkedExpr get _unlinkedConst => _unlinkedVariable?.initializer?.bodyExpr;
7387 } 7420 }
7388 7421
7389 /** 7422 /**
7390 * A concrete implementation of a [ParameterElement]. 7423 * A concrete implementation of a [ParameterElement].
7391 */ 7424 */
7392 class ParameterElementImpl extends VariableElementImpl 7425 class ParameterElementImpl extends VariableElementImpl
7393 with ParameterElementMixin 7426 with ParameterElementMixin
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after
7801 return new SourceRange( 7834 return new SourceRange(
7802 _unlinkedParam.visibleOffset, _unlinkedParam.visibleLength); 7835 _unlinkedParam.visibleOffset, _unlinkedParam.visibleLength);
7803 } 7836 }
7804 if (_visibleRangeLength < 0) { 7837 if (_visibleRangeLength < 0) {
7805 return null; 7838 return null;
7806 } 7839 }
7807 return new SourceRange(_visibleRangeOffset, _visibleRangeLength); 7840 return new SourceRange(_visibleRangeOffset, _visibleRangeLength);
7808 } 7841 }
7809 7842
7810 /** 7843 /**
7844 * Subclasses need this getter, see [ConstVariableElement._kernelInitializer].
7845 */
7846 kernel.Expression get _kernelInitializer => _kernel?.initializer;
7847
7848 /**
7811 * Subclasses need this getter, see [ConstVariableElement._unlinkedConst]. 7849 * Subclasses need this getter, see [ConstVariableElement._unlinkedConst].
7812 */ 7850 */
7813 UnlinkedExpr get _unlinkedConst => _unlinkedParam?.initializer?.bodyExpr; 7851 UnlinkedExpr get _unlinkedConst => _unlinkedParam?.initializer?.bodyExpr;
7814 7852
7815 @override 7853 @override
7816 T accept<T>(ElementVisitor<T> visitor) => visitor.visitParameterElement(this); 7854 T accept<T>(ElementVisitor<T> visitor) => visitor.visitParameterElement(this);
7817 7855
7818 @override 7856 @override
7819 void appendTo(StringBuffer buffer) { 7857 void appendTo(StringBuffer buffer) {
7820 String left = ""; 7858 String left = "";
(...skipping 1455 matching lines...) Expand 10 before | Expand all | Expand 10 after
9276 9314
9277 @override 9315 @override
9278 DartObject computeConstantValue() => null; 9316 DartObject computeConstantValue() => null;
9279 9317
9280 @override 9318 @override
9281 void visitChildren(ElementVisitor visitor) { 9319 void visitChildren(ElementVisitor visitor) {
9282 super.visitChildren(visitor); 9320 super.visitChildren(visitor);
9283 _initializer?.accept(visitor); 9321 _initializer?.accept(visitor);
9284 } 9322 }
9285 } 9323 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/src/summary/resynthesize_kernel_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698