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

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

Issue 1624853002: Store the result of type inference in summaries. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 11 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
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, 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 serialization.elements; 5 library serialization.elements;
6 6
7 import 'package:analyzer/dart/element/element.dart'; 7 import 'package:analyzer/dart/element/element.dart';
8 import 'package:analyzer/dart/element/type.dart'; 8 import 'package:analyzer/dart/element/type.dart';
9 import 'package:analyzer/src/dart/element/element.dart'; 9 import 'package:analyzer/src/dart/element/element.dart';
10 import 'package:analyzer/src/dart/element/type.dart'; 10 import 'package:analyzer/src/dart/element/type.dart';
11 import 'package:analyzer/src/generated/ast.dart'; 11 import 'package:analyzer/src/generated/ast.dart';
12 import 'package:analyzer/src/generated/resolver.dart'; 12 import 'package:analyzer/src/generated/resolver.dart';
13 import 'package:analyzer/src/generated/utilities_dart.dart'; 13 import 'package:analyzer/src/generated/utilities_dart.dart';
14 import 'package:analyzer/src/summary/format.dart'; 14 import 'package:analyzer/src/summary/format.dart';
15 import 'package:analyzer/src/summary/name_filter.dart'; 15 import 'package:analyzer/src/summary/name_filter.dart';
16 import 'package:analyzer/src/summary/summarize_const_expr.dart'; 16 import 'package:analyzer/src/summary/summarize_const_expr.dart';
17 17
18 /** 18 /**
19 * Serialize all the elements in [lib] to a summary using [ctx] as the context 19 * Serialize all the elements in [lib] to a summary using [ctx] as the context
20 * for building the summary, and using [typeProvider] to find built-in types. 20 * for building the summary, and using [typeProvider] to find built-in types.
21 */ 21 */
22 LibrarySerializationResult serializeLibrary( 22 LibrarySerializationResult serializeLibrary(
23 LibraryElement lib, TypeProvider typeProvider) { 23 LibraryElement lib, TypeProvider typeProvider, bool strongMode) {
24 var serializer = new _LibrarySerializer(lib, typeProvider); 24 var serializer = new _LibrarySerializer(lib, typeProvider, strongMode);
25 LinkedLibraryBuilder linked = serializer.serializeLibrary(); 25 LinkedLibraryBuilder linked = serializer.serializeLibrary();
26 return new LibrarySerializationResult( 26 return new LibrarySerializationResult(
27 linked, serializer.unlinkedUnits, serializer.unitUris); 27 linked, serializer.unlinkedUnits, serializer.unitUris);
28 } 28 }
29 29
30 ReferenceKind _getReferenceKind(Element element) { 30 ReferenceKind _getReferenceKind(Element element) {
31 ReferenceKind kind; 31 ReferenceKind kind;
32 if (element is PropertyAccessorElement) { 32 if (element is PropertyAccessorElement) {
33 kind = ReferenceKind.topLevelPropertyAccessor; 33 kind = ReferenceKind.topLevelPropertyAccessor;
34 } else if (element is FunctionTypeAliasElement) { 34 } else if (element is FunctionTypeAliasElement) {
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 } 433 }
434 434
435 /** 435 /**
436 * Serialize the given [executableElement], creating an [UnlinkedExecutable]. 436 * Serialize the given [executableElement], creating an [UnlinkedExecutable].
437 */ 437 */
438 UnlinkedExecutableBuilder serializeExecutable( 438 UnlinkedExecutableBuilder serializeExecutable(
439 ExecutableElement executableElement) { 439 ExecutableElement executableElement) {
440 UnlinkedExecutableBuilder b = new UnlinkedExecutableBuilder(); 440 UnlinkedExecutableBuilder b = new UnlinkedExecutableBuilder();
441 b.name = executableElement.name; 441 b.name = executableElement.name;
442 b.nameOffset = executableElement.nameOffset; 442 b.nameOffset = executableElement.nameOffset;
443 if (executableElement is! ConstructorElement && 443 if (executableElement is! ConstructorElement) {
444 !executableElement.hasImplicitReturnType) { 444 if (!executableElement.hasImplicitReturnType) {
445 b.returnType = serializeTypeRef( 445 b.returnType = serializeTypeRef(
446 executableElement.type.returnType, executableElement); 446 executableElement.type.returnType, executableElement);
447 } else if (!executableElement.isStatic) {
448 b.inferredReturnTypeSlot =
449 storeInferredType(executableElement.returnType, executableElement);
450 }
447 } 451 }
448 b.typeParameters = 452 b.typeParameters =
449 executableElement.typeParameters.map(serializeTypeParam).toList(); 453 executableElement.typeParameters.map(serializeTypeParam).toList();
450 b.parameters = 454 b.parameters =
451 executableElement.type.parameters.map(serializeParam).toList(); 455 executableElement.type.parameters.map(serializeParam).toList();
452 if (executableElement is PropertyAccessorElement) { 456 if (executableElement is PropertyAccessorElement) {
453 if (executableElement.isGetter) { 457 if (executableElement.isGetter) {
454 b.kind = UnlinkedExecutableKind.getter; 458 b.kind = UnlinkedExecutableKind.getter;
455 } else { 459 } else {
456 b.kind = UnlinkedExecutableKind.setter; 460 b.kind = UnlinkedExecutableKind.setter;
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 break; 535 break;
532 case ParameterKind.POSITIONAL: 536 case ParameterKind.POSITIONAL:
533 b.kind = UnlinkedParamKind.positional; 537 b.kind = UnlinkedParamKind.positional;
534 break; 538 break;
535 case ParameterKind.NAMED: 539 case ParameterKind.NAMED:
536 b.kind = UnlinkedParamKind.named; 540 b.kind = UnlinkedParamKind.named;
537 break; 541 break;
538 } 542 }
539 b.isInitializingFormal = parameter.isInitializingFormal; 543 b.isInitializingFormal = parameter.isInitializingFormal;
540 DartType type = parameter.type; 544 DartType type = parameter.type;
541 if (!parameter.hasImplicitType) { 545 if (parameter.hasImplicitType) {
546 Element contextParent = context.enclosingElement;
547 if (!parameter.isInitializingFormal &&
548 contextParent is ExecutableElement &&
549 !contextParent.isStatic &&
550 contextParent is! ConstructorElement) {
551 b.inferredTypeSlot = storeInferredType(type, context);
552 }
553 } else {
542 if (type is FunctionType) { 554 if (type is FunctionType) {
543 b.isFunctionTyped = true; 555 b.isFunctionTyped = true;
544 b.type = serializeTypeRef(type.returnType, parameter); 556 b.type = serializeTypeRef(type.returnType, parameter);
545 b.parameters = type.parameters 557 b.parameters = type.parameters
546 .map((parameter) => serializeParam(parameter, context)) 558 .map((parameter) => serializeParam(parameter, context))
547 .toList(); 559 .toList();
548 } else { 560 } else {
549 b.type = serializeTypeRef(type, context); 561 b.type = serializeTypeRef(type, context);
550 } 562 }
551 } 563 }
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
695 if (initializer != null) { 707 if (initializer != null) {
696 b.constExpr = serializeConstExpr(initializer); 708 b.constExpr = serializeConstExpr(initializer);
697 } 709 }
698 } 710 }
699 if (b.isFinal || b.isConst) { 711 if (b.isFinal || b.isConst) {
700 b.propagatedTypeSlot = storeLinkedType(variable.propagatedType, variable); 712 b.propagatedTypeSlot = storeLinkedType(variable.propagatedType, variable);
701 } else { 713 } else {
702 // Variable is not propagable. 714 // Variable is not propagable.
703 assert(variable.propagatedType == null); 715 assert(variable.propagatedType == null);
704 } 716 }
717 if (variable.hasImplicitType &&
718 (variable.initializer != null || !variable.isStatic)) {
719 b.inferredTypeSlot = storeInferredType(variable.type, variable);
720 }
705 return b; 721 return b;
706 } 722 }
707 723
708 /** 724 /**
725 * Create a slot id for the given [type] (which is an inferred type). If
726 * strong mode is enabled and [type] is not `dynamic`, it is stored in
727 * [linkedTypes] so that once the compilation unit has been fully visited, it
728 * will be serialized into [LinkedUnit.types].
729 *
730 * [context] is the element within which the slot id will appear; this is
731 * used to serialize type parameters.
732 */
733 int storeInferredType(DartType type, Element context) {
734 return storeLinkedType(
735 librarySerializer.strongMode && !type.isDynamic ? type : null, context);
736 }
737
738 /**
709 * Create a slot id for the given [type] (which may be either a propagated 739 * Create a slot id for the given [type] (which may be either a propagated
710 * type or an inferred type). If [type] is not `null`, it is stored in 740 * type or an inferred type). If [type] is not `null`, it is stored in
711 * [linkedTypes] so that once the compilation unit has been fully visited, 741 * [linkedTypes] so that once the compilation unit has been fully visited,
712 * it will be serialized to [LinkedUnit.types]. 742 * it will be serialized to [LinkedUnit.types].
713 * 743 *
714 * [context] is the element within which the slot id will appear; this is 744 * [context] is the element within which the slot id will appear; this is
715 * used to serialize type parameters. 745 * used to serialize type parameters.
716 */ 746 */
717 int storeLinkedType(DartType type, Element context) { 747 int storeLinkedType(DartType type, Element context) {
718 int slot = ++numSlots; 748 int slot = ++numSlots;
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
805 * The library to be serialized. 835 * The library to be serialized.
806 */ 836 */
807 final LibraryElement libraryElement; 837 final LibraryElement libraryElement;
808 838
809 /** 839 /**
810 * The type provider. This is used to locate the library for `dart:core`. 840 * The type provider. This is used to locate the library for `dart:core`.
811 */ 841 */
812 final TypeProvider typeProvider; 842 final TypeProvider typeProvider;
813 843
814 /** 844 /**
845 * Indicates whether the element model being serialized was analyzed using
846 * strong mode.
847 */
848 final bool strongMode;
849
850 /**
815 * Map from [LibraryElement] to the index of the entry in the "dependency 851 * Map from [LibraryElement] to the index of the entry in the "dependency
816 * table" that refers to it. 852 * table" that refers to it.
817 */ 853 */
818 final Map<LibraryElement, int> dependencyMap = <LibraryElement, int>{}; 854 final Map<LibraryElement, int> dependencyMap = <LibraryElement, int>{};
819 855
820 /** 856 /**
821 * The "dependency table". This is the list of objects which should be 857 * The "dependency table". This is the list of objects which should be
822 * written to [LinkedLibrary.dependencies]. 858 * written to [LinkedLibrary.dependencies].
823 */ 859 */
824 final List<LinkedDependencyBuilder> dependencies = 860 final List<LinkedDependencyBuilder> dependencies =
(...skipping 17 matching lines...) Expand all
842 * element; elements for which no prefix is needed are absent from this map. 878 * element; elements for which no prefix is needed are absent from this map.
843 */ 879 */
844 final Map<Element, PrefixElement> prefixMap = <Element, PrefixElement>{}; 880 final Map<Element, PrefixElement> prefixMap = <Element, PrefixElement>{};
845 881
846 /** 882 /**
847 * List of serializers for the compilation units constituting this library. 883 * List of serializers for the compilation units constituting this library.
848 */ 884 */
849 final List<_CompilationUnitSerializer> compilationUnitSerializers = 885 final List<_CompilationUnitSerializer> compilationUnitSerializers =
850 <_CompilationUnitSerializer>[]; 886 <_CompilationUnitSerializer>[];
851 887
852 _LibrarySerializer(this.libraryElement, this.typeProvider) { 888 _LibrarySerializer(this.libraryElement, this.typeProvider, this.strongMode) {
853 dependencies.add(new LinkedDependencyBuilder()); 889 dependencies.add(new LinkedDependencyBuilder());
854 dependencyMap[libraryElement] = 0; 890 dependencyMap[libraryElement] = 0;
855 } 891 }
856 892
857 /** 893 /**
858 * Retrieve a list of the URIs for the compilation units in the library. 894 * Retrieve a list of the URIs for the compilation units in the library.
859 */ 895 */
860 List<String> get unitUris => compilationUnitSerializers 896 List<String> get unitUris => compilationUnitSerializers
861 .map((_CompilationUnitSerializer s) => s.unitUri) 897 .map((_CompilationUnitSerializer s) => s.unitUri)
862 .toList(); 898 .toList();
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
973 exportNames.add(new LinkedExportNameBuilder( 1009 exportNames.add(new LinkedExportNameBuilder(
974 name: name, 1010 name: name,
975 dependency: serializeDependency(dependentLibrary), 1011 dependency: serializeDependency(dependentLibrary),
976 unit: unit, 1012 unit: unit,
977 kind: kind)); 1013 kind: kind));
978 } 1014 }
979 pb.exportNames = exportNames; 1015 pb.exportNames = exportNames;
980 return pb; 1016 return pb;
981 } 1017 }
982 } 1018 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698