OLD | NEW |
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 'dart:collection'; | 7 import 'dart:collection'; |
8 | 8 |
9 import 'package:analyzer/dart/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
10 import 'package:analyzer/dart/ast/standard_ast_factory.dart'; | 10 import 'package:analyzer/dart/ast/standard_ast_factory.dart'; |
11 import 'package:analyzer/dart/ast/token.dart'; | 11 import 'package:analyzer/dart/ast/token.dart'; |
12 import 'package:analyzer/dart/element/element.dart'; | 12 import 'package:analyzer/dart/element/element.dart'; |
13 import 'package:analyzer/dart/element/type.dart'; | 13 import 'package:analyzer/dart/element/type.dart'; |
14 import 'package:analyzer/src/dart/element/element.dart'; | 14 import 'package:analyzer/src/dart/element/element.dart'; |
15 import 'package:analyzer/src/dart/element/handle.dart'; | 15 import 'package:analyzer/src/dart/element/handle.dart'; |
16 import 'package:analyzer/src/dart/element/member.dart'; | 16 import 'package:analyzer/src/dart/element/member.dart'; |
17 import 'package:analyzer/src/dart/element/type.dart'; | 17 import 'package:analyzer/src/dart/element/type.dart'; |
18 import 'package:analyzer/src/generated/engine.dart'; | 18 import 'package:analyzer/src/generated/engine.dart'; |
19 import 'package:analyzer/src/generated/resolver.dart'; | 19 import 'package:analyzer/src/generated/resolver.dart'; |
20 import 'package:analyzer/src/generated/source_io.dart'; | 20 import 'package:analyzer/src/generated/source_io.dart'; |
21 import 'package:analyzer/src/generated/testing/ast_test_factory.dart'; | 21 import 'package:analyzer/src/generated/testing/ast_test_factory.dart'; |
22 import 'package:analyzer/src/generated/testing/token_factory.dart'; | 22 import 'package:analyzer/src/generated/testing/token_factory.dart'; |
23 import 'package:analyzer/src/summary/format.dart'; | 23 import 'package:analyzer/src/summary/format.dart'; |
24 import 'package:analyzer/src/summary/idl.dart'; | 24 import 'package:analyzer/src/summary/idl.dart'; |
| 25 import 'package:analyzer/src/summary/summary_sdk.dart'; |
25 | 26 |
26 /** | 27 /** |
27 * Implementation of [ElementResynthesizer] used when resynthesizing an element | 28 * Implementation of [ElementResynthesizer] used when resynthesizing an element |
28 * model from summaries. | 29 * model from summaries. |
29 */ | 30 */ |
30 abstract class SummaryResynthesizer extends ElementResynthesizer { | 31 abstract class SummaryResynthesizer extends ElementResynthesizer { |
31 /** | 32 /** |
32 * The parent [SummaryResynthesizer] which is asked to resynthesize elements | 33 * The parent [SummaryResynthesizer] which is asked to resynthesize elements |
33 * and get summaries before this resynthesizer attempts to do this. | 34 * and get summaries before this resynthesizer attempts to do this. |
34 * Can be `null`. | 35 * Can be `null`. |
35 */ | 36 */ |
36 final SummaryResynthesizer parent; | 37 final SummaryResynthesizer parent; |
37 | 38 |
38 /** | 39 /** |
39 * Source factory used to convert URIs to [Source] objects. | 40 * Source factory used to convert URIs to [Source] objects. |
40 */ | 41 */ |
41 final SourceFactory sourceFactory; | 42 final SourceFactory sourceFactory; |
42 | 43 |
43 /** | 44 /** |
44 * Cache of [Source] objects that have already been converted from URIs. | 45 * Cache of [Source] objects that have already been converted from URIs. |
45 */ | 46 */ |
46 final Map<String, Source> _sources = <String, Source>{}; | 47 final Map<String, Source> _sources = <String, Source>{}; |
47 | 48 |
48 /** | 49 /** |
49 * The [TypeProvider] used to obtain core types (such as Object, int, List, | 50 * The [TypeProvider] used to obtain SDK types during resynthesis. |
50 * and dynamic) during resynthesis. | |
51 */ | 51 */ |
52 final TypeProvider typeProvider; | 52 TypeProvider _typeProvider; |
53 | 53 |
54 /** | 54 /** |
55 * Indicates whether the summary should be resynthesized assuming strong mode | 55 * Indicates whether the summary should be resynthesized assuming strong mode |
56 * semantics. | 56 * semantics. |
57 */ | 57 */ |
58 final bool strongMode; | 58 final bool strongMode; |
59 | 59 |
60 /** | 60 /** |
61 * Map of compilation units resynthesized from summaries. The two map keys | 61 * Map of compilation units resynthesized from summaries. The two map keys |
62 * are the first two elements of the element's location (the library URI and | 62 * are the first two elements of the element's location (the library URI and |
(...skipping 10 matching lines...) Expand all Loading... |
73 final Map<String, Map<String, Map<String, Element>>> _resynthesizedElements = | 73 final Map<String, Map<String, Map<String, Element>>> _resynthesizedElements = |
74 <String, Map<String, Map<String, Element>>>{}; | 74 <String, Map<String, Map<String, Element>>>{}; |
75 | 75 |
76 /** | 76 /** |
77 * Map of libraries which have been resynthesized from summaries. The map | 77 * Map of libraries which have been resynthesized from summaries. The map |
78 * key is the library URI. | 78 * key is the library URI. |
79 */ | 79 */ |
80 final Map<String, LibraryElement> _resynthesizedLibraries = | 80 final Map<String, LibraryElement> _resynthesizedLibraries = |
81 <String, LibraryElement>{}; | 81 <String, LibraryElement>{}; |
82 | 82 |
83 SummaryResynthesizer(this.parent, AnalysisContext context, this.typeProvider, | 83 SummaryResynthesizer( |
84 this.sourceFactory, this.strongMode) | 84 this.parent, AnalysisContext context, this.sourceFactory, this.strongMode) |
85 : super(context); | 85 : super(context) { |
| 86 _buildTypeProvider(); |
| 87 } |
86 | 88 |
87 /** | 89 /** |
88 * Number of libraries that have been resynthesized so far. | 90 * Number of libraries that have been resynthesized so far. |
89 */ | 91 */ |
90 int get resynthesisCount => _resynthesizedLibraries.length; | 92 int get resynthesisCount => _resynthesizedLibraries.length; |
91 | 93 |
92 /** | 94 /** |
93 * Perform delayed finalization of the `dart:core` and `dart:async` libraries. | 95 * The [TypeProvider] used to obtain SDK types during resynthesis. |
94 */ | 96 */ |
95 void finalizeCoreAsyncLibraries() { | 97 TypeProvider get typeProvider => _typeProvider; |
96 (_resynthesizedLibraries['dart:core'] as LibraryElementImpl) | |
97 .createLoadLibraryFunction(typeProvider); | |
98 (_resynthesizedLibraries['dart:async'] as LibraryElementImpl) | |
99 .createLoadLibraryFunction(typeProvider); | |
100 } | |
101 | 98 |
102 @override | 99 @override |
103 Element getElement(ElementLocation location) { | 100 Element getElement(ElementLocation location) { |
104 List<String> components = location.components; | 101 List<String> components = location.components; |
105 String libraryUri = components[0]; | 102 String libraryUri = components[0]; |
106 // Ask the parent resynthesizer. | 103 // Ask the parent resynthesizer. |
107 if (parent != null && parent._hasLibrarySummary(libraryUri)) { | 104 if (parent != null && parent._hasLibrarySummary(libraryUri)) { |
108 return parent.getElement(location); | 105 return parent.getElement(location); |
109 } | 106 } |
110 // Resynthesize locally. | 107 // Resynthesize locally. |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 */ | 257 */ |
261 UnlinkedUnit getUnlinkedSummary(String uri); | 258 UnlinkedUnit getUnlinkedSummary(String uri); |
262 | 259 |
263 /** | 260 /** |
264 * Return `true` if this resynthesizer can provide summaries of the libraries | 261 * Return `true` if this resynthesizer can provide summaries of the libraries |
265 * with the given [uri]. Caller has already checked that | 262 * with the given [uri]. Caller has already checked that |
266 * `parent.hasLibrarySummary(uri)` returns `false`. | 263 * `parent.hasLibrarySummary(uri)` returns `false`. |
267 */ | 264 */ |
268 bool hasLibrarySummary(String uri); | 265 bool hasLibrarySummary(String uri); |
269 | 266 |
| 267 void _buildTypeProvider() { |
| 268 var coreLibrary = getLibraryElement('dart:core') as LibraryElementImpl; |
| 269 var asyncLibrary = getLibraryElement('dart:async') as LibraryElementImpl; |
| 270 SummaryTypeProvider summaryTypeProvider = new SummaryTypeProvider(); |
| 271 summaryTypeProvider.initializeCore(coreLibrary); |
| 272 summaryTypeProvider.initializeAsync(asyncLibrary); |
| 273 coreLibrary.createLoadLibraryFunction(summaryTypeProvider); |
| 274 asyncLibrary.createLoadLibraryFunction(summaryTypeProvider); |
| 275 _typeProvider = summaryTypeProvider; |
| 276 } |
| 277 |
270 /** | 278 /** |
271 * Return the [LinkedLibrary] for the given [uri] or return `null` if it | 279 * Return the [LinkedLibrary] for the given [uri] or return `null` if it |
272 * could not be found. | 280 * could not be found. |
273 */ | 281 */ |
274 LinkedLibrary _getLinkedSummaryOrNull(String uri) { | 282 LinkedLibrary _getLinkedSummaryOrNull(String uri) { |
275 if (parent != null && parent._hasLibrarySummary(uri)) { | 283 if (parent != null && parent._hasLibrarySummary(uri)) { |
276 return parent._getLinkedSummaryOrNull(uri); | 284 return parent._getLinkedSummaryOrNull(uri); |
277 } | 285 } |
278 return getLinkedSummary(uri); | 286 return getLinkedSummary(uri); |
279 } | 287 } |
(...skipping 1629 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1909 static String _getElementIdentifier(String name, ReferenceKind kind) { | 1917 static String _getElementIdentifier(String name, ReferenceKind kind) { |
1910 if (kind == ReferenceKind.topLevelPropertyAccessor || | 1918 if (kind == ReferenceKind.topLevelPropertyAccessor || |
1911 kind == ReferenceKind.propertyAccessor) { | 1919 kind == ReferenceKind.propertyAccessor) { |
1912 if (!name.endsWith('=')) { | 1920 if (!name.endsWith('=')) { |
1913 return name + '?'; | 1921 return name + '?'; |
1914 } | 1922 } |
1915 } | 1923 } |
1916 return name; | 1924 return name; |
1917 } | 1925 } |
1918 } | 1926 } |
OLD | NEW |