Chromium Code Reviews

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

Issue 2987553002: Add import dependencies to Kernel libraries and use them to resynthesize ImportElement(s) in Analyz… (Closed)
Patch Set: Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
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 1891 matching lines...)
1902 * class. 1902 * class.
1903 */ 1903 */
1904 class ConstFieldElementImpl extends FieldElementImpl with ConstVariableElement { 1904 class ConstFieldElementImpl extends FieldElementImpl with ConstVariableElement {
1905 /** 1905 /**
1906 * Initialize a newly created synthetic field element to have the given 1906 * Initialize a newly created synthetic field element to have the given
1907 * [name] and [offset]. 1907 * [name] and [offset].
1908 */ 1908 */
1909 ConstFieldElementImpl(String name, int offset) : super(name, offset); 1909 ConstFieldElementImpl(String name, int offset) : super(name, offset);
1910 1910
1911 /** 1911 /**
1912 * Initialize using the given kernel.
1913 */
1914 ConstFieldElementImpl.forKernel(
1915 ElementImpl enclosingElement, kernel.Field kernel)
1916 : super.forKernel(enclosingElement, kernel);
1917
1918 /**
1912 * Initialize a newly created field element to have the given [name]. 1919 * Initialize a newly created field element to have the given [name].
1913 */ 1920 */
1914 ConstFieldElementImpl.forNode(Identifier name) : super.forNode(name); 1921 ConstFieldElementImpl.forNode(Identifier name) : super.forNode(name);
1915 1922
1916 /** 1923 /**
1917 * Initialize using the given serialized information. 1924 * Initialize using the given serialized information.
1918 */ 1925 */
1919 ConstFieldElementImpl.forSerialized( 1926 ConstFieldElementImpl.forSerialized(
1920 UnlinkedVariable unlinkedVariable, ElementImpl enclosingElement) 1927 UnlinkedVariable unlinkedVariable, ElementImpl enclosingElement)
1921 : super.forSerialized(unlinkedVariable, enclosingElement); 1928 : super.forSerialized(unlinkedVariable, enclosingElement);
1922
1923 /**
1924 * Initialize using the given kernel.
1925 */
1926 ConstFieldElementImpl.forKernel(
1927 ElementImpl enclosingElement, kernel.Field kernel)
1928 : super.forKernel(enclosingElement, kernel);
1929 } 1929 }
1930 1930
1931 /** 1931 /**
1932 * A field element representing an enum constant. 1932 * A field element representing an enum constant.
1933 */ 1933 */
1934 class ConstFieldElementImpl_EnumValue extends ConstFieldElementImpl_ofEnum { 1934 class ConstFieldElementImpl_EnumValue extends ConstFieldElementImpl_ofEnum {
1935 final UnlinkedEnumValue _unlinkedEnumValue; 1935 final UnlinkedEnumValue _unlinkedEnumValue;
1936 final int _index; 1936 final int _index;
1937 1937
1938 ConstFieldElementImpl_EnumValue( 1938 ConstFieldElementImpl_EnumValue(
(...skipping 3535 matching lines...)
5474 * The unlinked representation of the import in the summary. 5474 * The unlinked representation of the import in the summary.
5475 */ 5475 */
5476 final UnlinkedImport _unlinkedImport; 5476 final UnlinkedImport _unlinkedImport;
5477 5477
5478 /** 5478 /**
5479 * The index of the dependency in the `imports` list. 5479 * The index of the dependency in the `imports` list.
5480 */ 5480 */
5481 final int _linkedDependency; 5481 final int _linkedDependency;
5482 5482
5483 /** 5483 /**
5484 * The kernel of the element.
5485 */
5486 final kernel.LibraryDependency _kernel;
5487
5488 /**
5484 * The offset of the prefix of this import in the file that contains the this 5489 * The offset of the prefix of this import in the file that contains the this
5485 * import directive, or `-1` if this import is synthetic. 5490 * import directive, or `-1` if this import is synthetic.
5486 */ 5491 */
5487 int _prefixOffset = 0; 5492 int _prefixOffset = 0;
5488 5493
5489 /** 5494 /**
5490 * The library that is imported into this library by this import directive. 5495 * The library that is imported into this library by this import directive.
5491 */ 5496 */
5492 LibraryElement _importedLibrary; 5497 LibraryElement _importedLibrary;
5493 5498
5494 /** 5499 /**
5495 * The combinators that were specified as part of the import directive in the 5500 * The combinators that were specified as part of the import directive in the
5496 * order in which they were specified. 5501 * order in which they were specified.
5497 */ 5502 */
5498 List<NamespaceCombinator> _combinators; 5503 List<NamespaceCombinator> _combinators;
5499 5504
5500 /** 5505 /**
5501 * The prefix that was specified as part of the import directive, or `null` if 5506 * The prefix that was specified as part of the import directive, or `null` if
5502 * there was no prefix specified. 5507 * there was no prefix specified.
5503 */ 5508 */
5504 PrefixElement _prefix; 5509 PrefixElement _prefix;
5505 5510
5506 /** 5511 /**
5507 * Initialize a newly created import element at the given [offset]. 5512 * Initialize a newly created import element at the given [offset].
5508 * The offset may be `-1` if the import is synthetic. 5513 * The offset may be `-1` if the import is synthetic.
5509 */ 5514 */
5510 ImportElementImpl(int offset) 5515 ImportElementImpl(int offset)
5511 : _unlinkedImport = null, 5516 : _unlinkedImport = null,
5512 _linkedDependency = null, 5517 _linkedDependency = null,
5518 _kernel = null,
5513 super(null, offset); 5519 super(null, offset);
5514 5520
5515 /** 5521 /**
5522 * Initialize using the given kernel.
5523 */
5524 ImportElementImpl.forKernel(LibraryElementImpl enclosingLibrary, this._kernel)
5525 : _unlinkedImport = null,
5526 _linkedDependency = null,
5527 super.forSerialized(enclosingLibrary);
5528
5529 /**
5516 * Initialize using the given serialized information. 5530 * Initialize using the given serialized information.
5517 */ 5531 */
5518 ImportElementImpl.forSerialized(this._unlinkedImport, this._linkedDependency, 5532 ImportElementImpl.forSerialized(this._unlinkedImport, this._linkedDependency,
5519 LibraryElementImpl enclosingLibrary) 5533 LibraryElementImpl enclosingLibrary)
5520 : super.forSerialized(enclosingLibrary); 5534 : _kernel = null,
5535 super.forSerialized(enclosingLibrary);
5521 5536
5522 @override 5537 @override
5523 List<NamespaceCombinator> get combinators { 5538 List<NamespaceCombinator> get combinators {
5524 if (_unlinkedImport != null && _combinators == null) { 5539 if (_unlinkedImport != null && _combinators == null) {
5525 _combinators = _buildCombinators(_unlinkedImport.combinators); 5540 _combinators = _buildCombinators(_unlinkedImport.combinators);
5526 } 5541 }
5527 return _combinators ?? const <NamespaceCombinator>[]; 5542 return _combinators ?? const <NamespaceCombinator>[];
5528 } 5543 }
5529 5544
5530 void set combinators(List<NamespaceCombinator> combinators) { 5545 void set combinators(List<NamespaceCombinator> combinators) {
5531 _assertNotResynthesized(_unlinkedImport); 5546 _assertNotResynthesized(_unlinkedImport);
5532 _combinators = combinators; 5547 _combinators = combinators;
5533 } 5548 }
5534 5549
5535 /** 5550 /**
5536 * Set whether this import is for a deferred library. 5551 * Set whether this import is for a deferred library.
5537 */ 5552 */
5538 void set deferred(bool isDeferred) { 5553 void set deferred(bool isDeferred) {
5539 _assertNotResynthesized(_unlinkedImport); 5554 _assertNotResynthesized(_unlinkedImport);
5540 setModifier(Modifier.DEFERRED, isDeferred); 5555 setModifier(Modifier.DEFERRED, isDeferred);
5541 } 5556 }
5542 5557
5543 @override 5558 @override
5544 String get identifier => "${importedLibrary.identifier}@$nameOffset"; 5559 String get identifier => "${importedLibrary.identifier}@$nameOffset";
5545 5560
5546 @override 5561 @override
5547 LibraryElement get importedLibrary { 5562 LibraryElement get importedLibrary {
5563 if (_kernel != null) {
5564 if (_importedLibrary == null) {
5565 Uri importedUri = _kernel.targetLibrary.importUri;
5566 String importedUriStr = importedUri.toString();
5567 LibraryElementImpl library = enclosingElement as LibraryElementImpl;
5568 _importedLibrary = library._kernelContext.getLibrary(importedUriStr);
5569 }
5570 }
5548 if (_linkedDependency != null) { 5571 if (_linkedDependency != null) {
5549 if (_importedLibrary == null) { 5572 if (_importedLibrary == null) {
5550 LibraryElementImpl library = enclosingElement as LibraryElementImpl; 5573 LibraryElementImpl library = enclosingElement as LibraryElementImpl;
5551 if (_linkedDependency == 0) { 5574 if (_linkedDependency == 0) {
5552 _importedLibrary = library; 5575 _importedLibrary = library;
5553 } else { 5576 } else {
5554 _importedLibrary = library.resynthesizerContext 5577 _importedLibrary = library.resynthesizerContext
5555 .buildImportedLibrary(_linkedDependency); 5578 .buildImportedLibrary(_linkedDependency);
5556 } 5579 }
5557 } 5580 }
(...skipping 125 matching lines...)
5683 */ 5706 */
5684 Expression getExpression(kernel.Expression expression); 5707 Expression getExpression(kernel.Expression expression);
5685 5708
5686 /** 5709 /**
5687 * Return the [InterfaceType] for the given Kernel [type], or `null` if the 5710 * Return the [InterfaceType] for the given Kernel [type], or `null` if the
5688 * [type] does not correspond to an [InterfaceType]. 5711 * [type] does not correspond to an [InterfaceType].
5689 */ 5712 */
5690 InterfaceType getInterfaceType(ElementImpl context, kernel.Supertype type); 5713 InterfaceType getInterfaceType(ElementImpl context, kernel.Supertype type);
5691 5714
5692 /** 5715 /**
5716 * Return the [LibraryElement] for the given absolute [uriStr].
5717 */
5718 LibraryElement getLibrary(String uriStr);
5719
5720 /**
5693 * Return the [DartType] for the given Kernel [type], or `null` if the [type] 5721 * Return the [DartType] for the given Kernel [type], or `null` if the [type]
5694 * does not correspond to a [DartType]. 5722 * does not correspond to a [DartType].
5695 */ 5723 */
5696 DartType getType(ElementImpl context, kernel.DartType type); 5724 DartType getType(ElementImpl context, kernel.DartType type);
5697 } 5725 }
5698 5726
5699 /** 5727 /**
5700 * A concrete implementation of a [LabelElement]. 5728 * A concrete implementation of a [LabelElement].
5701 */ 5729 */
5702 class LabelElementImpl extends ElementImpl implements LabelElement { 5730 class LabelElementImpl extends ElementImpl implements LabelElement {
(...skipping 360 matching lines...)
6063 LibraryElement library = element.importedLibrary; 6091 LibraryElement library = element.importedLibrary;
6064 if (library != null) { 6092 if (library != null) {
6065 libraries.add(library); 6093 libraries.add(library);
6066 } 6094 }
6067 } 6095 }
6068 return libraries.toList(growable: false); 6096 return libraries.toList(growable: false);
6069 } 6097 }
6070 6098
6071 @override 6099 @override
6072 List<ImportElement> get imports { 6100 List<ImportElement> get imports {
6073 if (_unlinkedDefiningUnit != null && _imports == null) { 6101 if (_imports == null) {
6074 List<UnlinkedImport> unlinkedImports = _unlinkedDefiningUnit.imports; 6102 if (_kernelContext != null) {
6075 int length = unlinkedImports.length; 6103 _imports = _kernelContext.library.dependencies
6076 if (length != 0) { 6104 .where((k) => k.isImport)
6077 List<ImportElement> imports = new List<ImportElement>(); 6105 .map((k) => new ImportElementImpl.forKernel(this, k))
6078 LinkedLibrary linkedLibrary = resynthesizerContext.linkedLibrary; 6106 .toList(growable: false);
6079 for (int i = 0; i < length; i++) { 6107 }
6080 int dependency = linkedLibrary.importDependencies[i]; 6108 if (_unlinkedDefiningUnit != null) {
6081 ImportElementImpl importElement = new ImportElementImpl.forSerialized( 6109 List<UnlinkedImport> unlinkedImports = _unlinkedDefiningUnit.imports;
6082 unlinkedImports[i], dependency, library); 6110 int length = unlinkedImports.length;
6083 imports.add(importElement); 6111 if (length != 0) {
6112 List<ImportElement> imports = new List<ImportElement>();
6113 LinkedLibrary linkedLibrary = resynthesizerContext.linkedLibrary;
6114 for (int i = 0; i < length; i++) {
6115 int dependency = linkedLibrary.importDependencies[i];
6116 ImportElementImpl importElement =
6117 new ImportElementImpl.forSerialized(
6118 unlinkedImports[i], dependency, library);
6119 imports.add(importElement);
6120 }
6121 _imports = imports;
6122 } else {
6123 _imports = const <ImportElement>[];
6084 } 6124 }
6085 _imports = imports;
6086 } else {
6087 _imports = const <ImportElement>[];
6088 } 6125 }
6089 } 6126 }
6090 return _imports ?? ImportElement.EMPTY_LIST; 6127 return _imports ?? ImportElement.EMPTY_LIST;
6091 } 6128 }
6092 6129
6093 /** 6130 /**
6094 * Set the specifications of all of the imports defined in this library to the 6131 * Set the specifications of all of the imports defined in this library to the
6095 * given list of [imports]. 6132 * given list of [imports].
6096 */ 6133 */
6097 void set imports(List<ImportElement> imports) { 6134 void set imports(List<ImportElement> imports) {
(...skipping 3148 matching lines...)
9246 9283
9247 @override 9284 @override
9248 DartObject computeConstantValue() => null; 9285 DartObject computeConstantValue() => null;
9249 9286
9250 @override 9287 @override
9251 void visitChildren(ElementVisitor visitor) { 9288 void visitChildren(ElementVisitor visitor) {
9252 super.visitChildren(visitor); 9289 super.visitChildren(visitor);
9253 _initializer?.accept(visitor); 9290 _initializer?.accept(visitor);
9254 } 9291 }
9255 } 9292 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/src/summary/resynthesize_kernel_test.dart » ('j') | pkg/kernel/lib/binary/ast_from_binary.dart » ('J')

Powered by Google App Engine