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

Side by Side Diff: pkg/analyzer/lib/src/summary/link.dart

Issue 1951833003: Use property accessor elements for type inference rather than top level variable elements. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 7 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 | no next file » | 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) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 /** 5 /**
6 * This library is capable of producing linked summaries from unlinked 6 * This library is capable of producing linked summaries from unlinked
7 * ones (or prelinked ones). It functions by building a miniature 7 * ones (or prelinked ones). It functions by building a miniature
8 * element model to represent the contents of the summaries, and then 8 * element model to represent the contents of the summaries, and then
9 * scanning the element model to gather linked information and adding 9 * scanning the element model to gather linked information and adding
10 * it to the summary data structures. 10 * it to the summary data structures.
(...skipping 710 matching lines...) Expand 10 before | Expand all | Expand 10 after
721 new PropertyAccessorElementForLink_Executable( 721 new PropertyAccessorElementForLink_Executable(
722 this, null, unlinkedExecutable, syntheticVariable); 722 this, null, unlinkedExecutable, syntheticVariable);
723 _accessors.add(accessor); 723 _accessors.add(accessor);
724 if (unlinkedExecutable.kind == UnlinkedExecutableKind.getter) { 724 if (unlinkedExecutable.kind == UnlinkedExecutableKind.getter) {
725 syntheticVariable._getter = accessor; 725 syntheticVariable._getter = accessor;
726 } else { 726 } else {
727 syntheticVariable._setter = accessor; 727 syntheticVariable._setter = accessor;
728 } 728 }
729 } 729 }
730 } 730 }
731 // TODO(paulberry): also add synthetic accessors. 731 for (TopLevelVariableElementForLink variable in topLevelVariables) {
732 _accessors.add(variable.getter);
733 if (!variable.isConst && !variable.isFinal) {
734 _accessors.add(variable.setter);
735 }
736 }
732 } 737 }
733 return _accessors; 738 return _accessors;
734 } 739 }
735 740
736 @override 741 @override
737 LibraryElementForLink get enclosingElement; 742 LibraryElementForLink get enclosingElement;
738 743
739 @override 744 @override
740 List<ClassElementForLink_Enum> get enums { 745 List<ClassElementForLink_Enum> get enums {
741 if (_enums == null) { 746 if (_enums == null) {
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
820 ReferenceableElementForLink getContainedName(name) { 825 ReferenceableElementForLink getContainedName(name) {
821 if (_containedNames == null) { 826 if (_containedNames == null) {
822 _containedNames = <String, ReferenceableElementForLink>{}; 827 _containedNames = <String, ReferenceableElementForLink>{};
823 // TODO(paulberry): what's the correct way to handle name conflicts? 828 // TODO(paulberry): what's the correct way to handle name conflicts?
824 for (ClassElementForLink_Class type in types) { 829 for (ClassElementForLink_Class type in types) {
825 _containedNames[type.name] = type; 830 _containedNames[type.name] = type;
826 } 831 }
827 for (ClassElementForLink_Enum enm in enums) { 832 for (ClassElementForLink_Enum enm in enums) {
828 _containedNames[enm.name] = enm; 833 _containedNames[enm.name] = enm;
829 } 834 }
830 for (TopLevelVariableElementForLink variable in topLevelVariables) {
831 _containedNames[variable.name] = variable;
832 }
833 for (TopLevelFunctionElementForLink function in functions) { 835 for (TopLevelFunctionElementForLink function in functions) {
834 _containedNames[function.name] = function; 836 _containedNames[function.name] = function;
835 } 837 }
836 for (PropertyAccessorElementForLink accessor in accessors) { 838 for (PropertyAccessorElementForLink accessor in accessors) {
837 // TODO(paulberry): consider handling synthetic accessors and getting 839 _containedNames[accessor.name] = accessor;
838 // rid of the loop above for topLevelVariables.
839 if (!accessor.isSynthetic) {
840 _containedNames[accessor.name] = accessor;
841 }
842 } 840 }
843 // TODO(paulberry): fill in other top level entities (typedefs 841 // TODO(paulberry): fill in other top level entities (typedefs
844 // and executables). 842 // and executables).
845 } 843 }
846 return _containedNames.putIfAbsent( 844 return _containedNames.putIfAbsent(
847 name, () => UndefinedElementForLink.instance); 845 name, () => UndefinedElementForLink.instance);
848 } 846 }
849 847
850 /** 848 /**
851 * Compute the type referred to by the given linked type [slot] (interpreted 849 * Compute the type referred to by the given linked type [slot] (interpreted
(...skipping 1605 matching lines...) Expand 10 before | Expand all | Expand 10 after
2457 } 2455 }
2458 2456
2459 /** 2457 /**
2460 * Specialization of [FieldElementForLink] for class fields. 2458 * Specialization of [FieldElementForLink] for class fields.
2461 */ 2459 */
2462 class FieldElementForLink_ClassField extends VariableElementForLink 2460 class FieldElementForLink_ClassField extends VariableElementForLink
2463 implements FieldElementForLink { 2461 implements FieldElementForLink {
2464 @override 2462 @override
2465 final ClassElementForLink_Class enclosingElement; 2463 final ClassElementForLink_Class enclosingElement;
2466 2464
2467 PropertyAccessorElementForLink_Variable _getter;
2468 PropertyAccessorElementForLink_Variable _setter;
2469
2470 /** 2465 /**
2471 * If this is an instance field, the type that was computed by 2466 * If this is an instance field, the type that was computed by
2472 * [InstanceMemberInferrer] (if any). Otherwise `null`. 2467 * [InstanceMemberInferrer] (if any). Otherwise `null`.
2473 */ 2468 */
2474 DartType _inferredInstanceType; 2469 DartType _inferredInstanceType;
2475 2470
2476 FieldElementForLink_ClassField(ClassElementForLink_Class enclosingElement, 2471 FieldElementForLink_ClassField(ClassElementForLink_Class enclosingElement,
2477 UnlinkedVariable unlinkedVariable) 2472 UnlinkedVariable unlinkedVariable)
2478 : enclosingElement = enclosingElement, 2473 : enclosingElement = enclosingElement,
2479 super(unlinkedVariable, enclosingElement.enclosingElement); 2474 super(unlinkedVariable, enclosingElement.enclosingElement);
2480 2475
2481 @override 2476 @override
2482 PropertyAccessorElementForLink_Variable get getter =>
2483 _getter ??= new PropertyAccessorElementForLink_Variable(this, false);
2484
2485 @override
2486 bool get isStatic => unlinkedVariable.isStatic; 2477 bool get isStatic => unlinkedVariable.isStatic;
2487 2478
2488 @override 2479 @override
2489 PropertyAccessorElementForLink_Variable get setter {
2490 if (!isConst && !isFinal) {
2491 return _setter ??=
2492 new PropertyAccessorElementForLink_Variable(this, true);
2493 } else {
2494 return null;
2495 }
2496 }
2497
2498 @override
2499 void set type(DartType inferredType) { 2480 void set type(DartType inferredType) {
2500 assert(!isStatic); 2481 assert(!isStatic);
2501 assert(_inferredInstanceType == null); 2482 assert(_inferredInstanceType == null);
2502 _inferredInstanceType = inferredType; 2483 _inferredInstanceType = inferredType;
2503 } 2484 }
2504 2485
2505 @override 2486 @override
2506 TypeParameterizedElementForLink get _typeParameterContext => enclosingElement; 2487 TypeParameterizedElementForLink get _typeParameterContext => enclosingElement;
2507 2488
2508 /** 2489 /**
(...skipping 1330 matching lines...) Expand 10 before | Expand all | Expand 10 after
3839 * Element representing a top level variable resynthesized from a 3820 * Element representing a top level variable resynthesized from a
3840 * summary during linking. 3821 * summary during linking.
3841 */ 3822 */
3842 class TopLevelVariableElementForLink extends VariableElementForLink 3823 class TopLevelVariableElementForLink extends VariableElementForLink
3843 implements TopLevelVariableElement { 3824 implements TopLevelVariableElement {
3844 TopLevelVariableElementForLink(CompilationUnitElementForLink enclosingElement, 3825 TopLevelVariableElementForLink(CompilationUnitElementForLink enclosingElement,
3845 UnlinkedVariable unlinkedVariable) 3826 UnlinkedVariable unlinkedVariable)
3846 : super(unlinkedVariable, enclosingElement); 3827 : super(unlinkedVariable, enclosingElement);
3847 3828
3848 @override 3829 @override
3830 CompilationUnitElementForLink get enclosingElement => compilationUnit;
3831
3832 @override
3849 bool get isStatic => true; 3833 bool get isStatic => true;
3850 3834
3851 @override 3835 @override
3852 LibraryElementForLink get library => compilationUnit.library; 3836 LibraryElementForLink get library => compilationUnit.library;
3853 3837
3854 @override 3838 @override
3855 TypeParameterizedElementForLink get _typeParameterContext => null; 3839 TypeParameterizedElementForLink get _typeParameterContext => null;
3856 3840
3857 /** 3841 /**
3858 * Store the results of type inference for this variable in 3842 * Store the results of type inference for this variable in
3859 * [compilationUnit]. 3843 * [compilationUnit].
3860 */ 3844 */
3861 void link(CompilationUnitElementInBuildUnit compilationUnit) { 3845 void link(CompilationUnitElementInBuildUnit compilationUnit) {
3862 if (hasImplicitType) { 3846 if (hasImplicitType) {
3863 TypeInferenceNode typeInferenceNode = this.asTypeInferenceNode; 3847 TypeInferenceNode typeInferenceNode = this._typeInferenceNode;
3864 if (typeInferenceNode != null) { 3848 if (typeInferenceNode != null) {
3865 compilationUnit._storeLinkedType( 3849 compilationUnit._storeLinkedType(
3866 unlinkedVariable.inferredTypeSlot, inferredType, null); 3850 unlinkedVariable.inferredTypeSlot, inferredType, null);
3867 } 3851 }
3868 } 3852 }
3869 } 3853 }
3870 } 3854 }
3871 3855
3872 /** 3856 /**
3873 * Specialization of [DependencyWalker] for performing type inferrence 3857 * Specialization of [DependencyWalker] for performing type inferrence
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
3969 } 3953 }
3970 3954
3971 void evaluate(bool inCycle) { 3955 void evaluate(bool inCycle) {
3972 if (inCycle) { 3956 if (inCycle) {
3973 variableElement._inferredType = DynamicTypeImpl.instance; 3957 variableElement._inferredType = DynamicTypeImpl.instance;
3974 } else { 3958 } else {
3975 variableElement._inferredType = 3959 variableElement._inferredType =
3976 new ExprTypeComputer(variableElement).compute(); 3960 new ExprTypeComputer(variableElement).compute();
3977 } 3961 }
3978 } 3962 }
3963
3964 @override
3965 String toString() => 'TypeInferenceNode($variableElement)';
3979 } 3966 }
3980 3967
3981 /** 3968 /**
3982 * Element representing a type parameter resynthesized from a summary during 3969 * Element representing a type parameter resynthesized from a summary during
3983 * linking. 3970 * linking.
3984 */ 3971 */
3985 class TypeParameterElementForLink implements TypeParameterElementImpl { 3972 class TypeParameterElementForLink implements TypeParameterElementImpl {
3986 /** 3973 /**
3987 * The unlinked representation of the type parameter in the summary. 3974 * The unlinked representation of the type parameter in the summary.
3988 */ 3975 */
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
4278 DynamicTypeImpl.instance; 4265 DynamicTypeImpl.instance;
4279 4266
4280 @override 4267 @override
4281 ReferenceableElementForLink getContainedName(String name) => this; 4268 ReferenceableElementForLink getContainedName(String name) => this;
4282 } 4269 }
4283 4270
4284 /** 4271 /**
4285 * Element representing a top level variable resynthesized from a 4272 * Element representing a top level variable resynthesized from a
4286 * summary during linking. 4273 * summary during linking.
4287 */ 4274 */
4288 class VariableElementForLink 4275 abstract class VariableElementForLink
4289 implements 4276 implements VariableElementImpl, PropertyInducingElement {
4290 VariableElementImpl,
4291 PropertyInducingElement,
4292 ReferenceableElementForLink {
4293 /** 4277 /**
4294 * The unlinked representation of the variable in the summary. 4278 * The unlinked representation of the variable in the summary.
4295 */ 4279 */
4296 final UnlinkedVariable unlinkedVariable; 4280 final UnlinkedVariable unlinkedVariable;
4297 4281
4298 /** 4282 /**
4299 * If this variable is declared `const` and the enclosing library is 4283 * If this variable is declared `const` and the enclosing library is
4300 * part of the build unit being linked, the variable's node in the 4284 * part of the build unit being linked, the variable's node in the
4301 * constant evaluation dependency graph. Otherwise `null`. 4285 * constant evaluation dependency graph. Otherwise `null`.
4302 */ 4286 */
4303 ConstNode _constNode; 4287 ConstNode _constNode;
4304 4288
4305 /** 4289 /**
4306 * If this variable has an initializer and an implicit type, and the enclosing 4290 * If this variable has an initializer and an implicit type, and the enclosing
4307 * library is part of the build unit being linked, the variable's node in the 4291 * library is part of the build unit being linked, the variable's node in the
4308 * type inference dependency graph. Otherwise `null`. 4292 * type inference dependency graph. Otherwise `null`.
4309 */ 4293 */
4310 TypeInferenceNode _typeInferenceNode; 4294 TypeInferenceNode _typeInferenceNode;
4311 4295
4312 FunctionElementForLink_Initializer _initializer; 4296 FunctionElementForLink_Initializer _initializer;
4313 DartType _inferredType; 4297 DartType _inferredType;
4314 DartType _declaredType; 4298 DartType _declaredType;
4299 PropertyAccessorElementForLink_Variable _getter;
4300 PropertyAccessorElementForLink_Variable _setter;
4315 4301
4316 /** 4302 /**
4317 * The compilation unit in which this variable appears. 4303 * The compilation unit in which this variable appears.
4318 */ 4304 */
4319 final CompilationUnitElementForLink compilationUnit; 4305 final CompilationUnitElementForLink compilationUnit;
4320 4306
4321 VariableElementForLink(this.unlinkedVariable, this.compilationUnit) { 4307 VariableElementForLink(this.unlinkedVariable, this.compilationUnit) {
4322 if (compilationUnit.isInBuildUnit && unlinkedVariable.constExpr != null) { 4308 if (compilationUnit.isInBuildUnit && unlinkedVariable.constExpr != null) {
4323 _constNode = new ConstVariableNode(this); 4309 _constNode = new ConstVariableNode(this);
4324 if (unlinkedVariable.type == null) { 4310 if (unlinkedVariable.type == null) {
4325 _typeInferenceNode = new TypeInferenceNode(this); 4311 _typeInferenceNode = new TypeInferenceNode(this);
4326 } 4312 }
4327 } 4313 }
4328 } 4314 }
4329 4315
4330 @override
4331 ConstructorElementForLink get asConstructor => null;
4332
4333 @override
4334 ConstVariableNode get asConstVariable => _constNode;
4335
4336 @override
4337 DartType get asStaticType => type;
4338
4339 @override
4340 TypeInferenceNode get asTypeInferenceNode => _typeInferenceNode;
4341
4342 /** 4316 /**
4343 * If the variable has an explicitly declared return type, return it. 4317 * If the variable has an explicitly declared return type, return it.
4344 * Otherwise return `null`. 4318 * Otherwise return `null`.
4345 */ 4319 */
4346 DartType get declaredType { 4320 DartType get declaredType {
4347 if (unlinkedVariable.type == null) { 4321 if (unlinkedVariable.type == null) {
4348 return null; 4322 return null;
4349 } else { 4323 } else {
4350 return _declaredType ??= compilationUnit._resolveTypeRef( 4324 return _declaredType ??= compilationUnit._resolveTypeRef(
4351 unlinkedVariable.type, _typeParameterContext); 4325 unlinkedVariable.type, _typeParameterContext);
4352 } 4326 }
4353 } 4327 }
4354 4328
4355 @override 4329 @override
4330 PropertyAccessorElementForLink_Variable get getter =>
4331 _getter ??= new PropertyAccessorElementForLink_Variable(this, false);
4332
4333 @override
4356 bool get hasImplicitType => unlinkedVariable.type == null; 4334 bool get hasImplicitType => unlinkedVariable.type == null;
4357 4335
4358 /** 4336 /**
4359 * Return the inferred type of the variable element. Should only be called if 4337 * Return the inferred type of the variable element. Should only be called if
4360 * no type was explicitly declared. 4338 * no type was explicitly declared.
4361 */ 4339 */
4362 DartType get inferredType { 4340 DartType get inferredType {
4363 // We should only try to infer a type when none is explicitly declared. 4341 // We should only try to infer a type when none is explicitly declared.
4364 assert(unlinkedVariable.type == null); 4342 assert(unlinkedVariable.type == null);
4365 if (_inferredType == null) { 4343 if (_inferredType == null) {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
4407 @override 4385 @override
4408 String get name => unlinkedVariable.name; 4386 String get name => unlinkedVariable.name;
4409 4387
4410 @override 4388 @override
4411 DartType get propagatedType { 4389 DartType get propagatedType {
4412 // TODO(paulberry): implement propagated types in the linker. 4390 // TODO(paulberry): implement propagated types in the linker.
4413 return DynamicTypeImpl.instance; 4391 return DynamicTypeImpl.instance;
4414 } 4392 }
4415 4393
4416 @override 4394 @override
4395 PropertyAccessorElementForLink_Variable get setter {
4396 if (!isConst && !isFinal) {
4397 return _setter ??=
4398 new PropertyAccessorElementForLink_Variable(this, true);
4399 } else {
4400 return null;
4401 }
4402 }
4403
4404 @override
4417 DartType get type => declaredType ?? inferredType; 4405 DartType get type => declaredType ?? inferredType;
4418 4406
4419 @override 4407 @override
4420 void set type(DartType newType) { 4408 void set type(DartType newType) {
4421 // TODO(paulberry): store inferred type. 4409 // TODO(paulberry): store inferred type.
4422 } 4410 }
4423 4411
4424 /** 4412 /**
4425 * The context in which type parameters should be interpreted, or `null` if 4413 * The context in which type parameters should be interpreted, or `null` if
4426 * there are no type parameters in scope. 4414 * there are no type parameters in scope.
4427 */ 4415 */
4428 TypeParameterizedElementForLink get _typeParameterContext; 4416 TypeParameterizedElementForLink get _typeParameterContext;
4429 4417
4430 @override 4418 @override
4431 DartType buildType(DartType getTypeArgument(int i), 4419 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
4432 List<int> implicitFunctionTypeIndices) =>
4433 DynamicTypeImpl.instance;
4434
4435 ReferenceableElementForLink getContainedName(String name) {
4436 return new NonstaticMemberElementForLink(library, this, name);
4437 }
4438 4420
4439 @override 4421 @override
4440 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); 4422 String toString() => '$enclosingElement.$name';
4441 } 4423 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698