OLD | NEW |
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 1597 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1608 * Initialize a newly created local variable element to have the given [name]. | 1608 * Initialize a newly created local variable element to have the given [name]. |
1609 */ | 1609 */ |
1610 ConstLocalVariableElementImpl.forNode(Identifier name) : super.forNode(name); | 1610 ConstLocalVariableElementImpl.forNode(Identifier name) : super.forNode(name); |
1611 | 1611 |
1612 /** | 1612 /** |
1613 * Initialize using the given serialized information. | 1613 * Initialize using the given serialized information. |
1614 */ | 1614 */ |
1615 ConstLocalVariableElementImpl.forSerialized(UnlinkedVariable unlinkedVariable, | 1615 ConstLocalVariableElementImpl.forSerialized(UnlinkedVariable unlinkedVariable, |
1616 ExecutableElementImpl enclosingExecutable) | 1616 ExecutableElementImpl enclosingExecutable) |
1617 : super.forSerialized(unlinkedVariable, enclosingExecutable); | 1617 : super.forSerialized(unlinkedVariable, enclosingExecutable); |
| 1618 |
| 1619 @override |
| 1620 Expression get constantInitializer { |
| 1621 if (_unlinkedVariable != null) { |
| 1622 UnlinkedConst defaultValue = _unlinkedVariable.initializer?.bodyExpr; |
| 1623 if (defaultValue == null) { |
| 1624 return null; |
| 1625 } |
| 1626 return super.constantInitializer ??= enclosingUnit.resynthesizerContext |
| 1627 .buildExpression(this, defaultValue); |
| 1628 } |
| 1629 return super.constantInitializer; |
| 1630 } |
| 1631 |
| 1632 @override |
| 1633 void set constantInitializer(Expression initializer) { |
| 1634 assert(_unlinkedVariable == null); |
| 1635 super.constantInitializer = initializer; |
| 1636 } |
1618 } | 1637 } |
1619 | 1638 |
1620 /** | 1639 /** |
1621 * A concrete implementation of a [ConstructorElement]. | 1640 * A concrete implementation of a [ConstructorElement]. |
1622 */ | 1641 */ |
1623 class ConstructorElementImpl extends ExecutableElementImpl | 1642 class ConstructorElementImpl extends ExecutableElementImpl |
1624 implements ConstructorElement { | 1643 implements ConstructorElement { |
1625 /** | 1644 /** |
1626 * The constructor to which this constructor is redirecting. | 1645 * The constructor to which this constructor is redirecting. |
1627 */ | 1646 */ |
(...skipping 1207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2835 | 2854 |
2836 /** | 2855 /** |
2837 * A list containing all of the labels defined within this executable element. | 2856 * A list containing all of the labels defined within this executable element. |
2838 */ | 2857 */ |
2839 List<LabelElement> _labels = LabelElement.EMPTY_LIST; | 2858 List<LabelElement> _labels = LabelElement.EMPTY_LIST; |
2840 | 2859 |
2841 /** | 2860 /** |
2842 * A list containing all of the local variables defined within this executable | 2861 * A list containing all of the local variables defined within this executable |
2843 * element. | 2862 * element. |
2844 */ | 2863 */ |
2845 List<LocalVariableElement> _localVariables = LocalVariableElement.EMPTY_LIST; | 2864 List<LocalVariableElement> _localVariables; |
2846 | 2865 |
2847 /** | 2866 /** |
2848 * A list containing all of the parameters defined by this executable element. | 2867 * A list containing all of the parameters defined by this executable element. |
2849 */ | 2868 */ |
2850 List<ParameterElement> _parameters; | 2869 List<ParameterElement> _parameters; |
2851 | 2870 |
2852 /** | 2871 /** |
2853 * A list containing all of the type parameters defined for this executable | 2872 * A list containing all of the type parameters defined for this executable |
2854 * element. | 2873 * element. |
2855 */ | 2874 */ |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3032 * [labels]. | 3051 * [labels]. |
3033 */ | 3052 */ |
3034 void set labels(List<LabelElement> labels) { | 3053 void set labels(List<LabelElement> labels) { |
3035 for (LabelElement label in labels) { | 3054 for (LabelElement label in labels) { |
3036 (label as LabelElementImpl).enclosingElement = this; | 3055 (label as LabelElementImpl).enclosingElement = this; |
3037 } | 3056 } |
3038 this._labels = labels; | 3057 this._labels = labels; |
3039 } | 3058 } |
3040 | 3059 |
3041 @override | 3060 @override |
3042 List<LocalVariableElement> get localVariables => _localVariables; | 3061 List<LocalVariableElement> get localVariables { |
| 3062 if (serializedExecutable != null && _localVariables == null) { |
| 3063 List<UnlinkedVariable> unlinkedVariables = |
| 3064 serializedExecutable.localVariables; |
| 3065 int length = unlinkedVariables.length; |
| 3066 if (length != 0) { |
| 3067 List<LocalVariableElementImpl> localVariables = |
| 3068 new List<LocalVariableElementImpl>(length); |
| 3069 for (int i = 0; i < length; i++) { |
| 3070 localVariables[i] = new LocalVariableElementImpl.forSerializedFactory( |
| 3071 unlinkedVariables[i], this); |
| 3072 } |
| 3073 _localVariables = localVariables; |
| 3074 } else { |
| 3075 _localVariables = const <LocalVariableElement>[]; |
| 3076 } |
| 3077 } |
| 3078 return _localVariables ?? const <LocalVariableElement>[]; |
| 3079 } |
3043 | 3080 |
3044 /** | 3081 /** |
3045 * Set the local variables defined within this executable element to the given | 3082 * Set the local variables defined within this executable element to the given |
3046 * [variables]. | 3083 * [variables]. |
3047 */ | 3084 */ |
3048 void set localVariables(List<LocalVariableElement> variables) { | 3085 void set localVariables(List<LocalVariableElement> variables) { |
| 3086 assert(serializedExecutable == null); |
3049 for (LocalVariableElement variable in variables) { | 3087 for (LocalVariableElement variable in variables) { |
3050 (variable as LocalVariableElementImpl).enclosingElement = this; | 3088 (variable as LocalVariableElementImpl).enclosingElement = this; |
3051 } | 3089 } |
3052 this._localVariables = variables; | 3090 this._localVariables = variables; |
3053 } | 3091 } |
3054 | 3092 |
3055 @override | 3093 @override |
3056 List<ElementAnnotation> get metadata { | 3094 List<ElementAnnotation> get metadata { |
3057 if (serializedExecutable != null) { | 3095 if (serializedExecutable != null) { |
3058 return _metadata ??= | 3096 return _metadata ??= |
(...skipping 2121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5180 */ | 5218 */ |
5181 LocalVariableElementImpl.forNode(Identifier name) : super.forNode(name); | 5219 LocalVariableElementImpl.forNode(Identifier name) : super.forNode(name); |
5182 | 5220 |
5183 /** | 5221 /** |
5184 * Initialize using the given serialized information. | 5222 * Initialize using the given serialized information. |
5185 */ | 5223 */ |
5186 LocalVariableElementImpl.forSerialized(UnlinkedVariable unlinkedVariable, | 5224 LocalVariableElementImpl.forSerialized(UnlinkedVariable unlinkedVariable, |
5187 ExecutableElementImpl enclosingExecutable) | 5225 ExecutableElementImpl enclosingExecutable) |
5188 : super.forSerialized(unlinkedVariable, enclosingExecutable); | 5226 : super.forSerialized(unlinkedVariable, enclosingExecutable); |
5189 | 5227 |
| 5228 /** |
| 5229 * Initialize using the given serialized information. |
| 5230 */ |
| 5231 factory LocalVariableElementImpl.forSerializedFactory( |
| 5232 UnlinkedVariable unlinkedVariable, |
| 5233 ExecutableElementImpl enclosingExecutable) { |
| 5234 if (unlinkedVariable.isConst && |
| 5235 unlinkedVariable.initializer?.bodyExpr != null) { |
| 5236 return new ConstLocalVariableElementImpl.forSerialized( |
| 5237 unlinkedVariable, enclosingExecutable); |
| 5238 } else { |
| 5239 return new LocalVariableElementImpl.forSerialized( |
| 5240 unlinkedVariable, enclosingExecutable); |
| 5241 } |
| 5242 } |
| 5243 |
5190 @override | 5244 @override |
5191 String get identifier { | 5245 String get identifier { |
5192 int enclosingOffset = | 5246 int enclosingOffset = |
5193 enclosingElement != null ? enclosingElement.nameOffset : 0; | 5247 enclosingElement != null ? enclosingElement.nameOffset : 0; |
5194 int delta = nameOffset - enclosingOffset; | 5248 int delta = nameOffset - enclosingOffset; |
5195 return '${super.identifier}@$delta'; | 5249 return '${super.identifier}@$delta'; |
5196 } | 5250 } |
5197 | 5251 |
5198 @override | 5252 @override |
5199 bool get isPotentiallyMutatedInClosure => true; | 5253 bool get isPotentiallyMutatedInClosure => true; |
5200 | 5254 |
5201 @override | 5255 @override |
5202 bool get isPotentiallyMutatedInScope => true; | 5256 bool get isPotentiallyMutatedInScope => true; |
5203 | 5257 |
5204 @override | 5258 @override |
5205 ElementKind get kind => ElementKind.LOCAL_VARIABLE; | 5259 ElementKind get kind => ElementKind.LOCAL_VARIABLE; |
5206 | 5260 |
5207 @override | 5261 @override |
5208 SourceRange get visibleRange { | 5262 SourceRange get visibleRange { |
| 5263 if (_unlinkedVariable != null) { |
| 5264 if (_unlinkedVariable.visibleLength == 0) { |
| 5265 return null; |
| 5266 } |
| 5267 return new SourceRange( |
| 5268 _unlinkedVariable.visibleOffset, _unlinkedVariable.visibleLength); |
| 5269 } |
5209 if (_visibleRangeLength < 0) { | 5270 if (_visibleRangeLength < 0) { |
5210 return null; | 5271 return null; |
5211 } | 5272 } |
5212 return new SourceRange(_visibleRangeOffset, _visibleRangeLength); | 5273 return new SourceRange(_visibleRangeOffset, _visibleRangeLength); |
5213 } | 5274 } |
5214 | 5275 |
5215 @override | 5276 @override |
5216 accept(ElementVisitor visitor) => visitor.visitLocalVariableElement(this); | 5277 accept(ElementVisitor visitor) => visitor.visitLocalVariableElement(this); |
5217 | 5278 |
5218 @override | 5279 @override |
5219 void appendTo(StringBuffer buffer) { | 5280 void appendTo(StringBuffer buffer) { |
5220 buffer.write(type); | 5281 buffer.write(type); |
5221 buffer.write(" "); | 5282 buffer.write(" "); |
5222 buffer.write(displayName); | 5283 buffer.write(displayName); |
5223 } | 5284 } |
5224 | 5285 |
5225 @override | 5286 @override |
5226 Declaration computeNode() => getNodeMatching( | 5287 Declaration computeNode() => getNodeMatching( |
5227 (node) => node is DeclaredIdentifier || node is VariableDeclaration); | 5288 (node) => node is DeclaredIdentifier || node is VariableDeclaration); |
5228 | 5289 |
5229 /** | 5290 /** |
5230 * Set the visible range for this element to the range starting at the given | 5291 * Set the visible range for this element to the range starting at the given |
5231 * [offset] with the given [length]. | 5292 * [offset] with the given [length]. |
5232 */ | 5293 */ |
5233 void setVisibleRange(int offset, int length) { | 5294 void setVisibleRange(int offset, int length) { |
| 5295 assert(_unlinkedVariable == null); |
5234 _visibleRangeOffset = offset; | 5296 _visibleRangeOffset = offset; |
5235 _visibleRangeLength = length; | 5297 _visibleRangeLength = length; |
5236 } | 5298 } |
5237 } | 5299 } |
5238 | 5300 |
5239 /** | 5301 /** |
5240 * A concrete implementation of a [MethodElement]. | 5302 * A concrete implementation of a [MethodElement]. |
5241 */ | 5303 */ |
5242 class MethodElementImpl extends ExecutableElementImpl implements MethodElement { | 5304 class MethodElementImpl extends ExecutableElementImpl implements MethodElement { |
5243 /** | 5305 /** |
(...skipping 581 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5825 return super.hasImplicitType; | 5887 return super.hasImplicitType; |
5826 } | 5888 } |
5827 | 5889 |
5828 @override | 5890 @override |
5829 void set hasImplicitType(bool hasImplicitType) { | 5891 void set hasImplicitType(bool hasImplicitType) { |
5830 assert(_unlinkedVariable == null); | 5892 assert(_unlinkedVariable == null); |
5831 super.hasImplicitType = hasImplicitType; | 5893 super.hasImplicitType = hasImplicitType; |
5832 } | 5894 } |
5833 | 5895 |
5834 @override | 5896 @override |
| 5897 FunctionElement get initializer { |
| 5898 if (_unlinkedVariable != null && _initializer == null) { |
| 5899 _initializer = enclosingUnit.resynthesizerContext |
| 5900 .buildVariableInitializer(this, _unlinkedVariable.initializer); |
| 5901 } |
| 5902 return super.initializer; |
| 5903 } |
| 5904 |
| 5905 /** |
| 5906 * Set the function representing this variable's initializer to the given |
| 5907 * [function]. |
| 5908 */ |
| 5909 void set initializer(FunctionElement function) { |
| 5910 assert(_unlinkedVariable == null); |
| 5911 super.initializer = function; |
| 5912 } |
| 5913 |
| 5914 @override |
5835 bool get isConst { | 5915 bool get isConst { |
5836 if (_unlinkedVariable != null) { | 5916 if (_unlinkedVariable != null) { |
5837 return _unlinkedVariable.isConst; | 5917 return _unlinkedVariable.isConst; |
5838 } | 5918 } |
5839 return super.isConst; | 5919 return super.isConst; |
5840 } | 5920 } |
5841 | 5921 |
5842 @override | 5922 @override |
5843 bool get isFinal { | 5923 bool get isFinal { |
5844 if (_unlinkedVariable != null) { | 5924 if (_unlinkedVariable != null) { |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6024 return super.hasImplicitType; | 6104 return super.hasImplicitType; |
6025 } | 6105 } |
6026 | 6106 |
6027 @override | 6107 @override |
6028 void set hasImplicitType(bool hasImplicitType) { | 6108 void set hasImplicitType(bool hasImplicitType) { |
6029 assert(_unlinkedParam == null); | 6109 assert(_unlinkedParam == null); |
6030 super.hasImplicitType = hasImplicitType; | 6110 super.hasImplicitType = hasImplicitType; |
6031 } | 6111 } |
6032 | 6112 |
6033 @override | 6113 @override |
| 6114 FunctionElement get initializer { |
| 6115 if (_unlinkedParam != null && _initializer == null) { |
| 6116 _initializer = enclosingUnit.resynthesizerContext |
| 6117 .buildVariableInitializer(this, _unlinkedParam.initializer); |
| 6118 } |
| 6119 return super.initializer; |
| 6120 } |
| 6121 |
| 6122 /** |
| 6123 * Set the function representing this variable's initializer to the given |
| 6124 * [function]. |
| 6125 */ |
| 6126 void set initializer(FunctionElement function) { |
| 6127 assert(_unlinkedParam == null); |
| 6128 super.initializer = function; |
| 6129 } |
| 6130 |
| 6131 @override |
6034 bool get isConst { | 6132 bool get isConst { |
6035 if (_unlinkedParam != null) { | 6133 if (_unlinkedParam != null) { |
6036 return false; | 6134 return false; |
6037 } | 6135 } |
6038 return super.isConst; | 6136 return super.isConst; |
6039 } | 6137 } |
6040 | 6138 |
6041 @override | 6139 @override |
6042 bool get isFinal { | 6140 bool get isFinal { |
6043 if (_unlinkedParam != null) { | 6141 if (_unlinkedParam != null) { |
(...skipping 651 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6695 * Build top-level functions. | 6793 * Build top-level functions. |
6696 */ | 6794 */ |
6697 List<FunctionElementImpl> buildTopLevelFunctions(); | 6795 List<FunctionElementImpl> buildTopLevelFunctions(); |
6698 | 6796 |
6699 /** | 6797 /** |
6700 * Build explicit top-level variables. | 6798 * Build explicit top-level variables. |
6701 */ | 6799 */ |
6702 UnitExplicitTopLevelVariables buildTopLevelVariables(); | 6800 UnitExplicitTopLevelVariables buildTopLevelVariables(); |
6703 | 6801 |
6704 /** | 6802 /** |
| 6803 * If the given [serializedInitializer] is not `null`, create the |
| 6804 * corresponding [FunctionElementImpl] and set it for the [variable]. |
| 6805 * |
| 6806 * TODO(scheglov) get rid of this when all parts are lazy |
| 6807 */ |
| 6808 FunctionElementImpl buildVariableInitializer( |
| 6809 VariableElementImpl variable, UnlinkedExecutable serializedInitializer); |
| 6810 |
| 6811 /** |
6705 * Build the appropriate [DartType] object corresponding to a slot id in the | 6812 * Build the appropriate [DartType] object corresponding to a slot id in the |
6706 * [LinkedUnit.types] table. | 6813 * [LinkedUnit.types] table. |
6707 */ | 6814 */ |
6708 DartType resolveLinkedType( | 6815 DartType resolveLinkedType( |
6709 int slot, TypeParameterizedElementMixin typeParameterContext); | 6816 int slot, TypeParameterizedElementMixin typeParameterContext); |
6710 | 6817 |
6711 /** | 6818 /** |
6712 * Resolve an [EntityRef] into a type. If the reference is | 6819 * Resolve an [EntityRef] into a type. If the reference is |
6713 * unresolved, return [DynamicTypeImpl.instance]. | 6820 * unresolved, return [DynamicTypeImpl.instance]. |
6714 * | 6821 * |
(...skipping 651 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7366 | 7473 |
7367 @override | 7474 @override |
7368 void visitElement(Element element) { | 7475 void visitElement(Element element) { |
7369 int offset = element.nameOffset; | 7476 int offset = element.nameOffset; |
7370 if (offset != -1) { | 7477 if (offset != -1) { |
7371 map[offset] = element; | 7478 map[offset] = element; |
7372 } | 7479 } |
7373 super.visitElement(element); | 7480 super.visitElement(element); |
7374 } | 7481 } |
7375 } | 7482 } |
OLD | NEW |