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

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

Issue 1559123002: Clean up resynthesis of typeArguments. (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
« no previous file with comments | « pkg/analyzer/lib/src/dart/element/type.dart ('k') | 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) 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 summary_resynthesizer; 5 library summary_resynthesizer;
6 6
7 import 'package:analyzer/analyzer.dart'; 7 import 'package:analyzer/analyzer.dart';
8 import 'package:analyzer/src/generated/element.dart'; 8 import 'package:analyzer/src/generated/element.dart';
9 import 'package:analyzer/src/generated/element_handle.dart'; 9 import 'package:analyzer/src/generated/element_handle.dart';
10 import 'package:analyzer/src/generated/engine.dart'; 10 import 'package:analyzer/src/generated/engine.dart';
11 import 'package:analyzer/src/generated/resolver.dart'; 11 import 'package:analyzer/src/generated/resolver.dart';
12 import 'package:analyzer/src/generated/source_io.dart'; 12 import 'package:analyzer/src/generated/source_io.dart';
13 import 'package:analyzer/src/summary/format.dart'; 13 import 'package:analyzer/src/summary/format.dart';
14 14
15 /** 15 /**
16 * Callback used by [SummaryResynthesizer] to obtain the prelinked summary for 16 * Callback used by [SummaryResynthesizer] to obtain the prelinked summary for
17 * a given URI. 17 * a given URI.
18 */ 18 */
19 typedef PrelinkedLibrary GetPrelinkedSummaryCallback(String uri); 19 typedef PrelinkedLibrary GetPrelinkedSummaryCallback(String uri);
20 20
21 /** 21 /**
22 * Callback used by [SummaryResynthesizer] to obtain the unlinked summary for a 22 * Callback used by [SummaryResynthesizer] to obtain the unlinked summary for a
23 * given URI. 23 * given URI.
24 */ 24 */
25 typedef UnlinkedUnit GetUnlinkedSummaryCallback(String uri); 25 typedef UnlinkedUnit GetUnlinkedSummaryCallback(String uri);
26 26
27 /** 27 /**
28 * Specialization of [FunctionTypeImpl] used for function types resynthesized
29 * from summaries.
30 */
31 class ResynthesizedFunctionTypeImpl extends FunctionTypeImpl
32 with ResynthesizedType {
33 final SummaryResynthesizer summaryResynthesizer;
34
35 ResynthesizedFunctionTypeImpl(
36 FunctionTypeAliasElement element, String name, this.summaryResynthesizer)
37 : super.elementWithName(element, name);
38
39 int get _numTypeParameters {
40 FunctionTypeAliasElement element = this.element;
41 return element.typeParameters.length;
42 }
43 }
44
45 /**
46 * Specialization of [InterfaceTypeImpl] used for interface types resynthesized
47 * from summaries.
48 */
49 class ResynthesizedInterfaceTypeImpl extends InterfaceTypeImpl
50 with ResynthesizedType {
51 final SummaryResynthesizer summaryResynthesizer;
52
53 ResynthesizedInterfaceTypeImpl(
54 ClassElement element, String name, this.summaryResynthesizer)
55 : super.elementWithName(element, name);
56
57 int get _numTypeParameters => element.typeParameters.length;
58 }
59
60 /**
61 * Common code for types resynthesized from summaries. This code takes care of
62 * filling in the appropriate number of copies of `dynamic` when it is queried
63 * for type parameters on a bare type reference (i.e. it converts `List` to
64 * `List<dynamic>`).
65 */
66 abstract class ResynthesizedType implements DartType {
67 /**
68 * The type arguments, if known. Otherwise `null`.
69 */
70 List<DartType> _typeArguments;
71
72 SummaryResynthesizer get summaryResynthesizer;
73
74 List<DartType> get typeArguments {
75 if (_typeArguments == null) {
76 // Default to replicating "dynamic" as many times as the class element
77 // requires.
78 _typeArguments = new List<DartType>.filled(
79 _numTypeParameters, summaryResynthesizer.typeProvider.dynamicType);
80 }
81 return _typeArguments;
82 }
83
84 int get _numTypeParameters;
85 }
86
87 /**
88 * Implementation of [ElementResynthesizer] used when resynthesizing an element 28 * Implementation of [ElementResynthesizer] used when resynthesizing an element
89 * model from summaries. 29 * model from summaries.
90 */ 30 */
91 class SummaryResynthesizer extends ElementResynthesizer { 31 class SummaryResynthesizer extends ElementResynthesizer {
92 /** 32 /**
93 * Callback used to obtain the prelinked summary for a given URI. 33 * Callback used to obtain the prelinked summary for a given URI.
94 */ 34 */
95 final GetPrelinkedSummaryCallback getPrelinkedSummary; 35 final GetPrelinkedSummaryCallback getPrelinkedSummary;
96 36
97 /** 37 /**
(...skipping 707 matching lines...) Expand 10 before | Expand all | Expand 10 after
805 if (referenceResolution.unit != 0) { 745 if (referenceResolution.unit != 0) {
806 String uri = unlinkedUnits[0].publicNamespace.parts[ 746 String uri = unlinkedUnits[0].publicNamespace.parts[
807 referenceResolution.unit - 1].uri; 747 referenceResolution.unit - 1].uri;
808 Source partSource = 748 Source partSource =
809 summaryResynthesizer.sourceFactory.resolveUri(librarySource, uri); 749 summaryResynthesizer.sourceFactory.resolveUri(librarySource, uri);
810 partUri = partSource.uri.toString(); 750 partUri = partSource.uri.toString();
811 } else { 751 } else {
812 partUri = referencedLibraryUri; 752 partUri = referencedLibraryUri;
813 } 753 }
814 } 754 }
815 ResynthesizedType resynthesizedType;
816 ElementLocationImpl location = new ElementLocationImpl.con3( 755 ElementLocationImpl location = new ElementLocationImpl.con3(
817 <String>[referencedLibraryUri, partUri, reference.name]); 756 <String>[referencedLibraryUri, partUri, reference.name]);
757 List<DartType> typeArguments = const <DartType>[];
758 if (referenceResolution.numTypeParameters != 0) {
759 typeArguments = <DartType>[];
760 for (int i = 0; i < referenceResolution.numTypeParameters; i++) {
761 if (i < type.typeArguments.length) {
762 typeArguments.add(buildType(type.typeArguments[i]));
763 } else {
764 typeArguments.add(summaryResynthesizer.typeProvider.dynamicType);
765 }
766 }
767 }
818 switch (referenceResolution.kind) { 768 switch (referenceResolution.kind) {
819 case PrelinkedReferenceKind.classOrEnum: 769 case PrelinkedReferenceKind.classOrEnum:
820 resynthesizedType = new ResynthesizedInterfaceTypeImpl( 770 return new InterfaceTypeImpl.elementWithNameAndArgs(
821 new ClassElementHandle(summaryResynthesizer, location), 771 new ClassElementHandle(summaryResynthesizer, location),
822 reference.name, 772 reference.name,
823 summaryResynthesizer); 773 typeArguments);
824 break;
825 case PrelinkedReferenceKind.typedef: 774 case PrelinkedReferenceKind.typedef:
826 resynthesizedType = new ResynthesizedFunctionTypeImpl( 775 return new FunctionTypeImpl.elementWithNameAndArgs(
827 new FunctionTypeAliasElementHandle( 776 new FunctionTypeAliasElementHandle(
828 summaryResynthesizer, location), 777 summaryResynthesizer, location),
829 reference.name, 778 reference.name,
830 summaryResynthesizer); 779 typeArguments);
831 break;
832 default: 780 default:
833 // TODO(paulberry): figure out how to handle this case (which should 781 // TODO(paulberry): figure out how to handle this case (which should
834 // only occur in the event of erroneous code). 782 // only occur in the event of erroneous code).
835 throw new UnimplementedError(); 783 throw new UnimplementedError();
836 } 784 }
837 if (type.typeArguments.isNotEmpty) {
838 resynthesizedType._typeArguments =
839 type.typeArguments.map(buildType).toList();
840 }
841 return resynthesizedType;
842 } 785 }
843 } 786 }
844 787
845 /** 788 /**
846 * Resynthesize a [FunctionTypeAliasElement] and place it in the 789 * Resynthesize a [FunctionTypeAliasElement] and place it in the
847 * [unitHolder]. 790 * [unitHolder].
848 */ 791 */
849 void buildTypedef(UnlinkedTypedef serializedTypedef) { 792 void buildTypedef(UnlinkedTypedef serializedTypedef) {
850 try { 793 try {
851 currentTypeParameters = 794 currentTypeParameters =
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
964 } 907 }
965 for (FunctionTypeAliasElement typeAlias in unit.functionTypeAliases) { 908 for (FunctionTypeAliasElement typeAlias in unit.functionTypeAliases) {
966 elementMap[typeAlias.name] = typeAlias; 909 elementMap[typeAlias.name] = typeAlias;
967 } 910 }
968 resummarizedElements[absoluteUri] = elementMap; 911 resummarizedElements[absoluteUri] = elementMap;
969 unitHolder = null; 912 unitHolder = null;
970 prelinkedUnit = null; 913 prelinkedUnit = null;
971 unlinkedUnit = null; 914 unlinkedUnit = null;
972 } 915 }
973 } 916 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/dart/element/type.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698