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

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

Issue 2040673002: Resynthesize enum fields lazily. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 6 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/summary/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';
11 import 'package:analyzer/dart/ast/token.dart'; 11 import 'package:analyzer/dart/ast/token.dart';
12 import 'package:analyzer/dart/constant/value.dart'; 12 import 'package:analyzer/dart/constant/value.dart';
13 import 'package:analyzer/dart/element/element.dart'; 13 import 'package:analyzer/dart/element/element.dart';
14 import 'package:analyzer/dart/element/type.dart'; 14 import 'package:analyzer/dart/element/type.dart';
15 import 'package:analyzer/dart/element/visitor.dart'; 15 import 'package:analyzer/dart/element/visitor.dart';
16 import 'package:analyzer/src/dart/ast/utilities.dart'; 16 import 'package:analyzer/src/dart/ast/utilities.dart';
17 import 'package:analyzer/src/dart/constant/value.dart';
17 import 'package:analyzer/src/dart/element/handle.dart'; 18 import 'package:analyzer/src/dart/element/handle.dart';
18 import 'package:analyzer/src/dart/element/type.dart'; 19 import 'package:analyzer/src/dart/element/type.dart';
19 import 'package:analyzer/src/generated/constant.dart' show EvaluationResultImpl; 20 import 'package:analyzer/src/generated/constant.dart' show EvaluationResultImpl;
20 import 'package:analyzer/src/generated/engine.dart' 21 import 'package:analyzer/src/generated/engine.dart'
21 show AnalysisContext, AnalysisEngine; 22 show AnalysisContext, AnalysisEngine;
22 import 'package:analyzer/src/generated/error.dart' show CompileTimeErrorCode; 23 import 'package:analyzer/src/generated/error.dart' show CompileTimeErrorCode;
23 import 'package:analyzer/src/generated/java_core.dart'; 24 import 'package:analyzer/src/generated/java_core.dart';
24 import 'package:analyzer/src/generated/java_engine.dart'; 25 import 'package:analyzer/src/generated/java_engine.dart';
25 import 'package:analyzer/src/generated/resolver.dart'; 26 import 'package:analyzer/src/generated/resolver.dart';
26 import 'package:analyzer/src/generated/sdk.dart' show DartSdk; 27 import 'package:analyzer/src/generated/sdk.dart' show DartSdk;
(...skipping 1753 matching lines...) Expand 10 before | Expand all | Expand 10 after
1780 1781
1781 /** 1782 /**
1782 * Initialize using the given serialized information. 1783 * Initialize using the given serialized information.
1783 */ 1784 */
1784 ConstFieldElementImpl.forSerialized( 1785 ConstFieldElementImpl.forSerialized(
1785 UnlinkedVariable unlinkedVariable, ElementImpl enclosingElement) 1786 UnlinkedVariable unlinkedVariable, ElementImpl enclosingElement)
1786 : super.forSerialized(unlinkedVariable, enclosingElement); 1787 : super.forSerialized(unlinkedVariable, enclosingElement);
1787 } 1788 }
1788 1789
1789 /** 1790 /**
1791 * A value field of an enum.
Brian Wilkerson 2016/06/06 13:39:50 This wasn't clear to me; I had to read more code t
scheglov 2016/06/06 15:31:43 Done.
1792 */
1793 class ConstFieldElementImpl_EnumValue extends ConstFieldElementImpl_ofEnum {
1794 final UnlinkedEnumValue _unlinkedEnumValue;
1795 final int _index;
1796
1797 ConstFieldElementImpl_EnumValue(
1798 EnumElementImpl enumElement, this._unlinkedEnumValue, this._index)
1799 : super(enumElement);
1800
1801 @override
1802 SourceRange get docRange {
1803 if (_unlinkedEnumValue != null) {
1804 UnlinkedDocumentationComment comment =
1805 _unlinkedEnumValue.documentationComment;
1806 return comment != null
1807 ? new SourceRange(comment.offset, comment.length)
1808 : null;
1809 }
1810 return super.docRange;
1811 }
1812
1813 @override
1814 String get documentationComment {
1815 if (_unlinkedEnumValue != null) {
1816 return _unlinkedEnumValue?.documentationComment?.text;
1817 }
1818 return super.documentationComment;
1819 }
1820
1821 @override
1822 EvaluationResultImpl get evaluationResult {
1823 if (_evaluationResult == null) {
1824 Map<String, DartObjectImpl> fieldMap = <String, DartObjectImpl>{
1825 name: new DartObjectImpl(
1826 context.typeProvider.intType, new IntState(_index))
1827 };
1828 DartObjectImpl value =
1829 new DartObjectImpl(type, new GenericState(fieldMap));
1830 _evaluationResult = new EvaluationResultImpl(value);
1831 }
1832 return _evaluationResult;
1833 }
1834
1835 @override
1836 String get name {
1837 if (_unlinkedEnumValue != null) {
1838 return _unlinkedEnumValue.name;
1839 }
1840 return super.name;
1841 }
1842
1843 @override
1844 int get nameOffset {
1845 if (_unlinkedEnumValue != null) {
1846 return _unlinkedEnumValue.nameOffset;
1847 }
1848 return super.nameOffset;
1849 }
1850
1851 @override
1852 InterfaceType get type => _enum.type;
1853 }
1854
1855 /**
1856 * The synthetic `values` field of an enum.
1857 */
1858 class ConstFieldElementImpl_EnumValues extends ConstFieldElementImpl_ofEnum {
1859 ConstFieldElementImpl_EnumValues(EnumElementImpl enumElement)
1860 : super(enumElement) {
1861 synthetic = true;
1862 }
1863
1864 @override
1865 EvaluationResultImpl get evaluationResult {
1866 if (_evaluationResult == null) {
1867 List<DartObjectImpl> constantValues = <DartObjectImpl>[];
1868 for (FieldElement field in _enum.fields) {
1869 if (field is ConstFieldElementImpl_EnumValue) {
1870 constantValues.add(field.evaluationResult.value);
1871 }
1872 }
1873 _evaluationResult = new EvaluationResultImpl(
1874 new DartObjectImpl(type, new ListState(constantValues)));
1875 }
1876 return _evaluationResult;
1877 }
1878
1879 @override
1880 String get name => 'values';
1881
1882 @override
1883 InterfaceType get type {
1884 if (_type == null) {
1885 InterfaceType listType = context.typeProvider.listType;
1886 return _type = listType.instantiate(<DartType>[_enum.type]);
1887 }
1888 return _type;
1889 }
1890 }
1891
1892 /**
1893 * An abstract constant field of an enum.
1894 */
1895 abstract class ConstFieldElementImpl_ofEnum extends ConstFieldElementImpl {
1896 final EnumElementImpl _enum;
1897
1898 ConstFieldElementImpl_ofEnum(this._enum) : super(null, -1) {
1899 enclosingElement = _enum;
1900 }
1901
1902 @override
1903 void set const3(bool isConst) {
1904 assert(false);
1905 }
1906
1907 @override
1908 void set evaluationResult(_) {
1909 assert(false);
1910 }
1911
1912 @override
1913 void set final2(bool isFinal) {
1914 assert(false);
1915 }
1916
1917 @override
1918 bool get isConst => true;
1919
1920 @override
1921 bool get isStatic => true;
1922
1923 @override
1924 void set static(bool isStatic) {
1925 assert(false);
1926 }
1927
1928 void set type(DartType type) {
1929 assert(false);
1930 }
1931 }
1932
1933 /**
1790 * A [LocalVariableElement] for a local 'const' variable that has an 1934 * A [LocalVariableElement] for a local 'const' variable that has an
1791 * initializer. 1935 * initializer.
1792 */ 1936 */
1793 class ConstLocalVariableElementImpl extends LocalVariableElementImpl 1937 class ConstLocalVariableElementImpl extends LocalVariableElementImpl
1794 with ConstVariableElement { 1938 with ConstVariableElement {
1795 /** 1939 /**
1796 * Initialize a newly created local variable element to have the given [name] 1940 * Initialize a newly created local variable element to have the given [name]
1797 * and [offset]. 1941 * and [offset].
1798 */ 1942 */
1799 ConstLocalVariableElementImpl(String name, int offset) : super(name, offset); 1943 ConstLocalVariableElementImpl(String name, int offset) : super(name, offset);
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after
2159 * If this element represents a constant variable, and it has an initializer, 2303 * If this element represents a constant variable, and it has an initializer,
2160 * a copy of the initializer for the constant. Otherwise `null`. 2304 * a copy of the initializer for the constant. Otherwise `null`.
2161 * 2305 *
2162 * Note that in correct Dart code, all constant variables must have 2306 * Note that in correct Dart code, all constant variables must have
2163 * initializers. However, analyzer also needs to handle incorrect Dart code, 2307 * initializers. However, analyzer also needs to handle incorrect Dart code,
2164 * in which case there might be some constant variables that lack 2308 * in which case there might be some constant variables that lack
2165 * initializers. 2309 * initializers.
2166 */ 2310 */
2167 Expression _constantInitializer; 2311 Expression _constantInitializer;
2168 2312
2169 @override 2313 EvaluationResultImpl _evaluationResult;
2170 EvaluationResultImpl evaluationResult;
2171 2314
2172 Expression get constantInitializer { 2315 Expression get constantInitializer {
2173 if (_constantInitializer == null && _unlinkedConst != null) { 2316 if (_constantInitializer == null && _unlinkedConst != null) {
2174 _constantInitializer = enclosingUnit.resynthesizerContext 2317 _constantInitializer = enclosingUnit.resynthesizerContext
2175 .buildExpression(this, _unlinkedConst); 2318 .buildExpression(this, _unlinkedConst);
2176 } 2319 }
2177 return _constantInitializer; 2320 return _constantInitializer;
2178 } 2321 }
2179 2322
2180 void set constantInitializer(Expression constantInitializer) { 2323 void set constantInitializer(Expression constantInitializer) {
2181 assert(_unlinkedConst == null); 2324 assert(_unlinkedConst == null);
2182 _constantInitializer = constantInitializer; 2325 _constantInitializer = constantInitializer;
2183 } 2326 }
2184 2327
2328 EvaluationResultImpl get evaluationResult => _evaluationResult;
2329
2330 void set evaluationResult(EvaluationResultImpl evaluationResult) {
2331 _evaluationResult = evaluationResult;
2332 }
2333
2185 /** 2334 /**
2186 * If this element is resynthesized from the summary, return the unlinked 2335 * If this element is resynthesized from the summary, return the unlinked
2187 * initializer, otherwise return `null`. 2336 * initializer, otherwise return `null`.
2188 */ 2337 */
2189 UnlinkedConst get _unlinkedConst; 2338 UnlinkedConst get _unlinkedConst;
2190 2339
2191 /** 2340 /**
2192 * Return a representation of the value of this variable, forcing the value 2341 * Return a representation of the value of this variable, forcing the value
2193 * to be computed if it had not previously been computed, or `null` if either 2342 * to be computed if it had not previously been computed, or `null` if either
2194 * this variable was not declared with the 'const' modifier or if the value of 2343 * this variable was not declared with the 'const' modifier or if the value of
(...skipping 989 matching lines...) Expand 10 before | Expand all | Expand 10 after
3184 : super.forSerialized(enclosingUnit); 3333 : super.forSerialized(enclosingUnit);
3185 3334
3186 /** 3335 /**
3187 * Set whether this class is abstract. 3336 * Set whether this class is abstract.
3188 */ 3337 */
3189 void set abstract(bool isAbstract) { 3338 void set abstract(bool isAbstract) {
3190 assert(_unlinkedEnum == null); 3339 assert(_unlinkedEnum == null);
3191 } 3340 }
3192 3341
3193 @override 3342 @override
3343 List<PropertyAccessorElement> get accessors {
3344 if (_unlinkedEnum != null && _accessors == null) {
3345 _resynthesizeFieldsAndPropertyAccessors();
3346 }
3347 return _accessors ?? const <PropertyAccessorElement>[];
3348 }
3349
3350 @override
3351 void set accessors(List<PropertyAccessorElement> accessors) {
3352 assert(_unlinkedEnum == null);
3353 super.accessors = accessors;
3354 }
3355
3356 @override
3194 List<InterfaceType> get allSupertypes => <InterfaceType>[supertype]; 3357 List<InterfaceType> get allSupertypes => <InterfaceType>[supertype];
3195 3358
3196 @override 3359 @override
3197 int get codeLength { 3360 int get codeLength {
3198 if (_unlinkedEnum != null) { 3361 if (_unlinkedEnum != null) {
3199 return _unlinkedEnum.codeRange?.length; 3362 return _unlinkedEnum.codeRange?.length;
3200 } 3363 }
3201 return super.codeLength; 3364 return super.codeLength;
3202 } 3365 }
3203 3366
(...skipping 27 matching lines...) Expand all
3231 3394
3232 @override 3395 @override
3233 String get documentationComment { 3396 String get documentationComment {
3234 if (_unlinkedEnum != null) { 3397 if (_unlinkedEnum != null) {
3235 return _unlinkedEnum?.documentationComment?.text; 3398 return _unlinkedEnum?.documentationComment?.text;
3236 } 3399 }
3237 return super.documentationComment; 3400 return super.documentationComment;
3238 } 3401 }
3239 3402
3240 @override 3403 @override
3404 List<FieldElement> get fields {
3405 if (_unlinkedEnum != null && _fields == null) {
3406 _resynthesizeFieldsAndPropertyAccessors();
3407 }
3408 return _fields ?? const <FieldElement>[];
3409 }
3410
3411 @override
3412 void set fields(List<FieldElement> fields) {
3413 assert(_unlinkedEnum == null);
3414 super.fields = fields;
3415 }
3416
3417 @override
3241 bool get hasNonFinalField => false; 3418 bool get hasNonFinalField => false;
3242 3419
3243 @override 3420 @override
3244 bool get hasReferenceToSuper => false; 3421 bool get hasReferenceToSuper => false;
3245 3422
3246 @override 3423 @override
3247 bool get hasStaticMember => true; 3424 bool get hasStaticMember => true;
3248 3425
3249 @override 3426 @override
3250 List<InterfaceType> get interfaces => const <InterfaceType>[]; 3427 List<InterfaceType> get interfaces => const <InterfaceType>[];
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
3330 } 3507 }
3331 3508
3332 @override 3509 @override
3333 MethodElement getMethod(String name) => null; 3510 MethodElement getMethod(String name) => null;
3334 3511
3335 @override 3512 @override
3336 ConstructorElement getNamedConstructor(String name) => null; 3513 ConstructorElement getNamedConstructor(String name) => null;
3337 3514
3338 @override 3515 @override
3339 bool isSuperConstructorAccessible(ConstructorElement constructor) => false; 3516 bool isSuperConstructorAccessible(ConstructorElement constructor) => false;
3517
3518 void _resynthesizeFieldsAndPropertyAccessors() {
3519 List<FieldElementImpl> fields = <FieldElementImpl>[];
3520 // Build the 'index' field.
3521 fields.add(new FieldElementImpl('index', -1)
3522 ..enclosingElement = this
3523 ..synthetic = true
3524 ..final2 = true
3525 ..type = context.typeProvider.intType);
3526 // Build the 'values' field.
3527 fields.add(new ConstFieldElementImpl_EnumValues(this));
3528 // Build fields for all enum constants.
3529 for (int i = 0; i < _unlinkedEnum.values.length; i++) {
3530 UnlinkedEnumValue unlinkedValue = _unlinkedEnum.values[i];
3531 ConstFieldElementImpl_EnumValue field =
3532 new ConstFieldElementImpl_EnumValue(this, unlinkedValue, i);
3533 fields.add(field);
3534 }
3535 // done
3536 _fields = fields;
3537 _accessors = fields
3538 .map((FieldElementImpl field) =>
3539 new PropertyAccessorElementImpl_ImplicitGetter(field)
3540 ..enclosingElement = this)
3541 .toList(growable: false);
3542 }
3340 } 3543 }
3341 3544
3342 /** 3545 /**
3343 * A base class for concrete implementations of an [ExecutableElement]. 3546 * A base class for concrete implementations of an [ExecutableElement].
3344 */ 3547 */
3345 abstract class ExecutableElementImpl extends ElementImpl 3548 abstract class ExecutableElementImpl extends ElementImpl
3346 with TypeParameterizedElementMixin 3549 with TypeParameterizedElementMixin
3347 implements ExecutableElement { 3550 implements ExecutableElement {
3348 /** 3551 /**
3349 * The unlinked representation of the executable in the summary. 3552 * The unlinked representation of the executable in the summary.
(...skipping 4070 matching lines...) Expand 10 before | Expand all | Expand 10 after
7420 */ 7623 */
7421 class PropertyAccessorElementImpl_ImplicitGetter 7624 class PropertyAccessorElementImpl_ImplicitGetter
7422 extends PropertyAccessorElementImpl { 7625 extends PropertyAccessorElementImpl {
7423 /** 7626 /**
7424 * Create the implicit getter and bind it to the [property]. 7627 * Create the implicit getter and bind it to the [property].
7425 */ 7628 */
7426 PropertyAccessorElementImpl_ImplicitGetter( 7629 PropertyAccessorElementImpl_ImplicitGetter(
7427 PropertyInducingElementImpl property) 7630 PropertyInducingElementImpl property)
7428 : super.forVariable(property) { 7631 : super.forVariable(property) {
7429 property.getter = this; 7632 property.getter = this;
7633 enclosingElement = property.enclosingElement;
7430 } 7634 }
7431 7635
7432 @override 7636 @override
7433 bool get hasImplicitReturnType => variable.hasImplicitType; 7637 bool get hasImplicitReturnType => variable.hasImplicitType;
7434 7638
7435 @override 7639 @override
7436 bool get isGetter => true; 7640 bool get isGetter => true;
7437 7641
7438 @override 7642 @override
7439 DartType get returnType => variable.type; 7643 DartType get returnType => variable.type;
(...skipping 796 matching lines...) Expand 10 before | Expand all | Expand 10 after
8236 8440
8237 @override 8441 @override
8238 void visitElement(Element element) { 8442 void visitElement(Element element) {
8239 int offset = element.nameOffset; 8443 int offset = element.nameOffset;
8240 if (offset != -1) { 8444 if (offset != -1) {
8241 map[offset] = element; 8445 map[offset] = element;
8242 } 8446 }
8243 super.visitElement(element); 8447 super.visitElement(element);
8244 } 8448 }
8245 } 8449 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/lib/src/summary/resynthesize.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698