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

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

Issue 2988113002: Add Class.isEnum to Kernel and use it to resynthesize enums in Analyzer. (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 1616 matching lines...) Expand 10 before | Expand all | Expand 10 after
1627 LibraryElement get enclosingElement => 1627 LibraryElement get enclosingElement =>
1628 super.enclosingElement as LibraryElement; 1628 super.enclosingElement as LibraryElement;
1629 1629
1630 @override 1630 @override
1631 CompilationUnitElementImpl get enclosingUnit { 1631 CompilationUnitElementImpl get enclosingUnit {
1632 return this; 1632 return this;
1633 } 1633 }
1634 1634
1635 @override 1635 @override
1636 List<ClassElement> get enums { 1636 List<ClassElement> get enums {
1637 if (_kernelContext != null) {
1638 _enums ??= _kernelContext.library.classes
1639 .where((k) => k.isEnum)
1640 .map((k) => new EnumElementImpl.forKernel(this, k))
1641 .toList(growable: false);
1642 }
1637 if (_unlinkedUnit != null) { 1643 if (_unlinkedUnit != null) {
1638 _enums ??= _unlinkedUnit.enums 1644 _enums ??= _unlinkedUnit.enums
1639 .map((e) => new EnumElementImpl.forSerialized(e, this)) 1645 .map((e) => new EnumElementImpl.forSerialized(e, this))
1640 .toList(growable: false); 1646 .toList(growable: false);
1641 } 1647 }
1642 return _enums ?? const <ClassElement>[]; 1648 return _enums ?? const <ClassElement>[];
1643 } 1649 }
1644 1650
1645 /** 1651 /**
1646 * Set the enums contained in this compilation unit to the given [enums]. 1652 * Set the enums contained in this compilation unit to the given [enums].
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
1786 this._typeAliases = typeAliases; 1792 this._typeAliases = typeAliases;
1787 } 1793 }
1788 1794
1789 @override 1795 @override
1790 TypeParameterizedElementMixin get typeParameterContext => null; 1796 TypeParameterizedElementMixin get typeParameterContext => null;
1791 1797
1792 @override 1798 @override
1793 List<ClassElement> get types { 1799 List<ClassElement> get types {
1794 if (_kernelContext != null) { 1800 if (_kernelContext != null) {
1795 _types ??= _kernelContext.library.classes 1801 _types ??= _kernelContext.library.classes
1796 .where((k) => !k.isSyntheticMixinImplementation) 1802 .where((k) => !k.isEnum && !k.isSyntheticMixinImplementation)
1797 .map((k) => new ClassElementImpl.forKernel(this, k)) 1803 .map((k) => new ClassElementImpl.forKernel(this, k))
1798 .toList(growable: false); 1804 .toList(growable: false);
1799 } 1805 }
1800 if (_unlinkedUnit != null) { 1806 if (_unlinkedUnit != null) {
1801 _types ??= _unlinkedUnit.classes 1807 _types ??= _unlinkedUnit.classes
1802 .map((c) => new ClassElementImpl.forSerialized(c, this)) 1808 .map((c) => new ClassElementImpl.forSerialized(c, this))
1803 .toList(growable: false); 1809 .toList(growable: false);
1804 } 1810 }
1805 return _types ?? const <ClassElement>[]; 1811 return _types ?? const <ClassElement>[];
1806 } 1812 }
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
1994 ConstFieldElementImpl.forSerialized( 2000 ConstFieldElementImpl.forSerialized(
1995 UnlinkedVariable unlinkedVariable, ElementImpl enclosingElement) 2001 UnlinkedVariable unlinkedVariable, ElementImpl enclosingElement)
1996 : super.forSerialized(unlinkedVariable, enclosingElement); 2002 : super.forSerialized(unlinkedVariable, enclosingElement);
1997 } 2003 }
1998 2004
1999 /** 2005 /**
2000 * A field element representing an enum constant. 2006 * A field element representing an enum constant.
2001 */ 2007 */
2002 class ConstFieldElementImpl_EnumValue extends ConstFieldElementImpl_ofEnum { 2008 class ConstFieldElementImpl_EnumValue extends ConstFieldElementImpl_ofEnum {
2003 final UnlinkedEnumValue _unlinkedEnumValue; 2009 final UnlinkedEnumValue _unlinkedEnumValue;
2010 final kernel.Field _kernelEnumValue;
2004 final int _index; 2011 final int _index;
2005 2012
2006 ConstFieldElementImpl_EnumValue( 2013 ConstFieldElementImpl_EnumValue(EnumElementImpl enumElement,
2007 EnumElementImpl enumElement, this._unlinkedEnumValue, this._index) 2014 this._unlinkedEnumValue, this._kernelEnumValue, this._index)
2008 : super(enumElement); 2015 : super(enumElement);
2009 2016
2010 @override 2017 @override
2011 String get documentationComment { 2018 String get documentationComment {
2019 if (_kernelEnumValue != null) {
2020 return _kernelEnumValue.documentationComment;
2021 }
2012 if (_unlinkedEnumValue != null) { 2022 if (_unlinkedEnumValue != null) {
2013 return _unlinkedEnumValue?.documentationComment?.text; 2023 return _unlinkedEnumValue?.documentationComment?.text;
2014 } 2024 }
2015 return super.documentationComment; 2025 return super.documentationComment;
2016 } 2026 }
2017 2027
2018 @override 2028 @override
2019 EvaluationResultImpl get evaluationResult { 2029 EvaluationResultImpl get evaluationResult {
2020 if (_evaluationResult == null) { 2030 if (_evaluationResult == null) {
2021 Map<String, DartObjectImpl> fieldMap = <String, DartObjectImpl>{ 2031 Map<String, DartObjectImpl> fieldMap = <String, DartObjectImpl>{
2022 name: new DartObjectImpl( 2032 name: new DartObjectImpl(
2023 context.typeProvider.intType, new IntState(_index)) 2033 context.typeProvider.intType, new IntState(_index))
2024 }; 2034 };
2025 DartObjectImpl value = 2035 DartObjectImpl value =
2026 new DartObjectImpl(type, new GenericState(fieldMap)); 2036 new DartObjectImpl(type, new GenericState(fieldMap));
2027 _evaluationResult = new EvaluationResultImpl(value); 2037 _evaluationResult = new EvaluationResultImpl(value);
2028 } 2038 }
2029 return _evaluationResult; 2039 return _evaluationResult;
2030 } 2040 }
2031 2041
2032 @override 2042 @override
2033 String get name { 2043 String get name {
2044 if (_kernelEnumValue != null) {
2045 return _kernelEnumValue.name.name;
2046 }
2034 if (_unlinkedEnumValue != null) { 2047 if (_unlinkedEnumValue != null) {
2035 return _unlinkedEnumValue.name; 2048 return _unlinkedEnumValue.name;
2036 } 2049 }
2037 return super.name; 2050 return super.name;
2038 } 2051 }
2039 2052
2040 @override 2053 @override
2041 int get nameOffset { 2054 int get nameOffset {
2042 int offset = super.nameOffset; 2055 int offset = super.nameOffset;
2043 if (offset == -1 && _unlinkedEnumValue != null) { 2056 if (offset == -1 && _unlinkedEnumValue != null) {
(...skipping 1576 matching lines...) Expand 10 before | Expand all | Expand 10 after
3620 /** 3633 /**
3621 * An [AbstractClassElementImpl] which is an enum. 3634 * An [AbstractClassElementImpl] which is an enum.
3622 */ 3635 */
3623 class EnumElementImpl extends AbstractClassElementImpl { 3636 class EnumElementImpl extends AbstractClassElementImpl {
3624 /** 3637 /**
3625 * The unlinked representation of the enum in the summary. 3638 * The unlinked representation of the enum in the summary.
3626 */ 3639 */
3627 final UnlinkedEnum _unlinkedEnum; 3640 final UnlinkedEnum _unlinkedEnum;
3628 3641
3629 /** 3642 /**
3643 * The kernel of the element.
3644 */
3645 final kernel.Class _kernel;
3646
3647 /**
3630 * The type defined by the enum. 3648 * The type defined by the enum.
3631 */ 3649 */
3632 InterfaceType _type; 3650 InterfaceType _type;
3633 3651
3634 /** 3652 /**
3635 * Initialize a newly created class element to have the given [name] at the 3653 * Initialize a newly created class element to have the given [name] at the
3636 * given [offset] in the file that contains the declaration of this element. 3654 * given [offset] in the file that contains the declaration of this element.
3637 */ 3655 */
3638 EnumElementImpl(String name, int offset) 3656 EnumElementImpl(String name, int offset)
3639 : _unlinkedEnum = null, 3657 : _unlinkedEnum = null,
3658 _kernel = null,
3640 super(name, offset); 3659 super(name, offset);
3641 3660
3642 /** 3661 /**
3662 * Initialize using the given kernel.
3663 */
3664 EnumElementImpl.forKernel(
3665 CompilationUnitElementImpl enclosingUnit, this._kernel)
3666 : _unlinkedEnum = null,
3667 super.forSerialized(enclosingUnit);
3668
3669 /**
3643 * Initialize a newly created class element to have the given [name]. 3670 * Initialize a newly created class element to have the given [name].
3644 */ 3671 */
3645 EnumElementImpl.forNode(Identifier name) 3672 EnumElementImpl.forNode(Identifier name)
3646 : _unlinkedEnum = null, 3673 : _unlinkedEnum = null,
3674 _kernel = null,
3647 super.forNode(name); 3675 super.forNode(name);
3648 3676
3649 /** 3677 /**
3650 * Initialize using the given serialized information. 3678 * Initialize using the given serialized information.
3651 */ 3679 */
3652 EnumElementImpl.forSerialized( 3680 EnumElementImpl.forSerialized(
3653 this._unlinkedEnum, CompilationUnitElementImpl enclosingUnit) 3681 this._unlinkedEnum, CompilationUnitElementImpl enclosingUnit)
3654 : super.forSerialized(enclosingUnit); 3682 : _kernel = null,
3683 super.forSerialized(enclosingUnit);
3655 3684
3656 /** 3685 /**
3657 * Set whether this class is abstract. 3686 * Set whether this class is abstract.
3658 */ 3687 */
3659 void set abstract(bool isAbstract) { 3688 void set abstract(bool isAbstract) {
3660 _assertNotResynthesized(_unlinkedEnum); 3689 _assertNotResynthesized(_unlinkedEnum);
3661 } 3690 }
3662 3691
3663 @override 3692 @override
3664 List<PropertyAccessorElement> get accessors { 3693 List<PropertyAccessorElement> get accessors {
3665 if (_unlinkedEnum != null && _accessors == null) { 3694 if (_accessors == null) {
3666 _resynthesizeFieldsAndPropertyAccessors(); 3695 if (_kernel != null || _unlinkedEnum != null) {
3696 _resynthesizeFieldsAndPropertyAccessors();
3697 }
3667 } 3698 }
3668 return _accessors ?? const <PropertyAccessorElement>[]; 3699 return _accessors ?? const <PropertyAccessorElement>[];
3669 } 3700 }
3670 3701
3671 @override 3702 @override
3672 void set accessors(List<PropertyAccessorElement> accessors) { 3703 void set accessors(List<PropertyAccessorElement> accessors) {
3673 _assertNotResynthesized(_unlinkedEnum); 3704 _assertNotResynthesized(_unlinkedEnum);
3674 super.accessors = accessors; 3705 super.accessors = accessors;
3675 } 3706 }
3676 3707
(...skipping 20 matching lines...) Expand all
3697 List<ConstructorElement> get constructors { 3728 List<ConstructorElement> get constructors {
3698 // The equivalent code for enums in the spec shows a single constructor, 3729 // The equivalent code for enums in the spec shows a single constructor,
3699 // but that constructor is not callable (since it is a compile-time error 3730 // but that constructor is not callable (since it is a compile-time error
3700 // to subclass, mix-in, implement, or explicitly instantiate an enum). 3731 // to subclass, mix-in, implement, or explicitly instantiate an enum).
3701 // So we represent this as having no constructors. 3732 // So we represent this as having no constructors.
3702 return const <ConstructorElement>[]; 3733 return const <ConstructorElement>[];
3703 } 3734 }
3704 3735
3705 @override 3736 @override
3706 String get documentationComment { 3737 String get documentationComment {
3738 if (_kernel != null) {
3739 return _kernel.documentationComment;
3740 }
3707 if (_unlinkedEnum != null) { 3741 if (_unlinkedEnum != null) {
3708 return _unlinkedEnum?.documentationComment?.text; 3742 return _unlinkedEnum?.documentationComment?.text;
3709 } 3743 }
3710 return super.documentationComment; 3744 return super.documentationComment;
3711 } 3745 }
3712 3746
3713 @override 3747 @override
3714 List<FieldElement> get fields { 3748 List<FieldElement> get fields {
3715 if (_unlinkedEnum != null && _fields == null) { 3749 if (_fields == null) {
3716 _resynthesizeFieldsAndPropertyAccessors(); 3750 if (_kernel != null || _unlinkedEnum != null) {
3751 _resynthesizeFieldsAndPropertyAccessors();
3752 }
3717 } 3753 }
3718 return _fields ?? const <FieldElement>[]; 3754 return _fields ?? const <FieldElement>[];
3719 } 3755 }
3720 3756
3721 @override 3757 @override
3722 void set fields(List<FieldElement> fields) { 3758 void set fields(List<FieldElement> fields) {
3723 _assertNotResynthesized(_unlinkedEnum); 3759 _assertNotResynthesized(_unlinkedEnum);
3724 super.fields = fields; 3760 super.fields = fields;
3725 } 3761 }
3726 3762
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
3764 } 3800 }
3765 3801
3766 @override 3802 @override
3767 List<MethodElement> get methods => const <MethodElement>[]; 3803 List<MethodElement> get methods => const <MethodElement>[];
3768 3804
3769 @override 3805 @override
3770 List<InterfaceType> get mixins => const <InterfaceType>[]; 3806 List<InterfaceType> get mixins => const <InterfaceType>[];
3771 3807
3772 @override 3808 @override
3773 String get name { 3809 String get name {
3810 if (_kernel != null) {
3811 return _kernel.name;
3812 }
3774 if (_unlinkedEnum != null) { 3813 if (_unlinkedEnum != null) {
3775 return _unlinkedEnum.name; 3814 return _unlinkedEnum.name;
3776 } 3815 }
3777 return super.name; 3816 return super.name;
3778 } 3817 }
3779 3818
3780 @override 3819 @override
3781 int get nameOffset { 3820 int get nameOffset {
3782 int offset = super.nameOffset; 3821 int offset = super.nameOffset;
3783 if (offset == 0 && _unlinkedEnum != null && _unlinkedEnum.nameOffset != 0) { 3822 if (offset == 0 && _unlinkedEnum != null && _unlinkedEnum.nameOffset != 0) {
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
3830 List<FieldElementImpl> fields = <FieldElementImpl>[]; 3869 List<FieldElementImpl> fields = <FieldElementImpl>[];
3831 // Build the 'index' field. 3870 // Build the 'index' field.
3832 fields.add(new FieldElementImpl('index', -1) 3871 fields.add(new FieldElementImpl('index', -1)
3833 ..enclosingElement = this 3872 ..enclosingElement = this
3834 ..isSynthetic = true 3873 ..isSynthetic = true
3835 ..isFinal = true 3874 ..isFinal = true
3836 ..type = context.typeProvider.intType); 3875 ..type = context.typeProvider.intType);
3837 // Build the 'values' field. 3876 // Build the 'values' field.
3838 fields.add(new ConstFieldElementImpl_EnumValues(this)); 3877 fields.add(new ConstFieldElementImpl_EnumValues(this));
3839 // Build fields for all enum constants. 3878 // Build fields for all enum constants.
3840 for (int i = 0; i < _unlinkedEnum.values.length; i++) { 3879 if (_kernel != null) {
3841 UnlinkedEnumValue unlinkedValue = _unlinkedEnum.values[i]; 3880 for (int i = 0; i < _kernel.fields.length; i++) {
3842 ConstFieldElementImpl_EnumValue field = 3881 kernel.Field kernelField = _kernel.fields[i];
3843 new ConstFieldElementImpl_EnumValue(this, unlinkedValue, i); 3882 if (kernelField.name.name == 'index' ||
3844 fields.add(field); 3883 kernelField.name.name == 'values') {
3884 continue;
3885 }
3886 ConstFieldElementImpl_EnumValue field =
3887 new ConstFieldElementImpl_EnumValue(this, null, kernelField, i);
3888 fields.add(field);
3889 }
3890 }
3891 if (_unlinkedEnum != null) {
3892 for (int i = 0; i < _unlinkedEnum.values.length; i++) {
3893 UnlinkedEnumValue unlinkedValue = _unlinkedEnum.values[i];
3894 ConstFieldElementImpl_EnumValue field =
3895 new ConstFieldElementImpl_EnumValue(this, unlinkedValue, null, i);
3896 fields.add(field);
3897 }
3845 } 3898 }
3846 // done 3899 // done
3847 _fields = fields; 3900 _fields = fields;
3848 _accessors = fields 3901 _accessors = fields
3849 .map((FieldElementImpl field) => 3902 .map((FieldElementImpl field) =>
3850 new PropertyAccessorElementImpl_ImplicitGetter(field) 3903 new PropertyAccessorElementImpl_ImplicitGetter(field)
3851 ..enclosingElement = this) 3904 ..enclosingElement = this)
3852 .toList(growable: false); 3905 .toList(growable: false);
3853 } 3906 }
3854 } 3907 }
(...skipping 2812 matching lines...) Expand 10 before | Expand all | Expand 10 after
6667 } 6720 }
6668 for (ExportElement exportElement in exports) { 6721 for (ExportElement exportElement in exports) {
6669 ExportElementImpl exportElementImpl = exportElement; 6722 ExportElementImpl exportElementImpl = exportElement;
6670 if (exportElementImpl.identifier == identifier) { 6723 if (exportElementImpl.identifier == identifier) {
6671 return exportElementImpl; 6724 return exportElementImpl;
6672 } 6725 }
6673 } 6726 }
6674 return null; 6727 return null;
6675 } 6728 }
6676 6729
6730 ClassElement getEnum(String name) {
6731 ClassElement element = _definingCompilationUnit.getEnum(name);
6732 if (element != null) {
6733 return element;
6734 }
6735 for (CompilationUnitElement part in _parts) {
6736 element = part.getEnum(name);
6737 if (element != null) {
6738 return element;
6739 }
6740 }
6741 return null;
6742 }
6743
6677 @override 6744 @override
6678 List<ImportElement> getImportsWithPrefix(PrefixElement prefixElement) { 6745 List<ImportElement> getImportsWithPrefix(PrefixElement prefixElement) {
6679 var imports = this.imports; 6746 var imports = this.imports;
6680 int count = imports.length; 6747 int count = imports.length;
6681 List<ImportElement> importList = new List<ImportElement>(); 6748 List<ImportElement> importList = new List<ImportElement>();
6682 for (int i = 0; i < count; i++) { 6749 for (int i = 0; i < count; i++) {
6683 if (identical(imports[i].prefix, prefixElement)) { 6750 if (identical(imports[i].prefix, prefixElement)) {
6684 importList.add(imports[i]); 6751 importList.add(imports[i]);
6685 } 6752 }
6686 } 6753 }
(...skipping 3020 matching lines...) Expand 10 before | Expand all | Expand 10 after
9707 9774
9708 @override 9775 @override
9709 DartObject computeConstantValue() => null; 9776 DartObject computeConstantValue() => null;
9710 9777
9711 @override 9778 @override
9712 void visitChildren(ElementVisitor visitor) { 9779 void visitChildren(ElementVisitor visitor) {
9713 super.visitChildren(visitor); 9780 super.visitChildren(visitor);
9714 _initializer?.accept(visitor); 9781 _initializer?.accept(visitor);
9715 } 9782 }
9716 } 9783 }
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