OLD | NEW |
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 library dart2js.serialization_helper; | 5 library dart2js.serialization_helper; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 import 'package:compiler/src/commandline_options.dart'; | 10 import 'package:compiler/src/commandline_options.dart'; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 Compiler compiler, | 97 Compiler compiler, |
98 Iterable<LibraryElement> libraries, | 98 Iterable<LibraryElement> libraries, |
99 {bool serializeResolvedAst: false}) { | 99 {bool serializeResolvedAst: false}) { |
100 assert(compiler.serialization.supportSerialization); | 100 assert(compiler.serialization.supportSerialization); |
101 | 101 |
102 Serializer serializer = new Serializer(); | 102 Serializer serializer = new Serializer(); |
103 serializer.plugins.add(compiler.backend.serialization.serializer); | 103 serializer.plugins.add(compiler.backend.serialization.serializer); |
104 serializer.plugins.add(new ResolutionImpactSerializer(compiler.resolution)); | 104 serializer.plugins.add(new ResolutionImpactSerializer(compiler.resolution)); |
105 if (serializeResolvedAst) { | 105 if (serializeResolvedAst) { |
106 serializer.plugins.add( | 106 serializer.plugins.add( |
107 new ResolvedAstSerializerPlugin(compiler.resolution)); | 107 new ResolvedAstSerializerPlugin(compiler.resolution, compiler.backend)); |
108 } | 108 } |
109 | 109 |
110 for (LibraryElement library in libraries) { | 110 for (LibraryElement library in libraries) { |
111 serializer.serialize(library); | 111 serializer.serialize(library); |
112 } | 112 } |
113 return serializer; | 113 return serializer; |
114 } | 114 } |
115 | 115 |
116 void deserialize(Compiler compiler, | 116 void deserialize(Compiler compiler, |
117 String serializedData, | 117 String serializedData, |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 final bool _deserializeResolvedAst; | 170 final bool _deserializeResolvedAst; |
171 | 171 |
172 _DeserializerSystem( | 172 _DeserializerSystem( |
173 Compiler compiler, | 173 Compiler compiler, |
174 this._deserializer, | 174 this._deserializer, |
175 this._impactTransformer, | 175 this._impactTransformer, |
176 {bool deserializeResolvedAst: false}) | 176 {bool deserializeResolvedAst: false}) |
177 : this._compiler = compiler, | 177 : this._compiler = compiler, |
178 this._deserializeResolvedAst = deserializeResolvedAst, | 178 this._deserializeResolvedAst = deserializeResolvedAst, |
179 this._resolvedAstDeserializer = deserializeResolvedAst | 179 this._resolvedAstDeserializer = deserializeResolvedAst |
180 ? new ResolvedAstDeserializerPlugin(compiler.parsingContext) : null { | 180 ? new ResolvedAstDeserializerPlugin( |
| 181 compiler.parsingContext, compiler.backend) : null { |
181 _deserializer.plugins.add(_resolutionImpactDeserializer); | 182 _deserializer.plugins.add(_resolutionImpactDeserializer); |
182 if (_deserializeResolvedAst) { | 183 if (_deserializeResolvedAst) { |
183 _deserializer.plugins.add(_resolvedAstDeserializer); | 184 _deserializer.plugins.add(_resolvedAstDeserializer); |
184 } | 185 } |
185 } | 186 } |
186 | 187 |
187 @override | 188 @override |
188 Future<LibraryElement> readLibrary(Uri resolvedUri) { | 189 Future<LibraryElement> readLibrary(Uri resolvedUri) { |
189 LibraryElement library = _deserializer.lookupLibrary(resolvedUri); | 190 LibraryElement library = _deserializer.lookupLibrary(resolvedUri); |
190 if (library != null) { | 191 if (library != null) { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 @override | 240 @override |
240 bool isDeserialized(Element element) { | 241 bool isDeserialized(Element element) { |
241 return deserializedLibraries.contains(element.library); | 242 return deserializedLibraries.contains(element.library); |
242 } | 243 } |
243 } | 244 } |
244 | 245 |
245 const String RESOLVED_AST_TAG = 'resolvedAst'; | 246 const String RESOLVED_AST_TAG = 'resolvedAst'; |
246 | 247 |
247 class ResolvedAstSerializerPlugin extends SerializerPlugin { | 248 class ResolvedAstSerializerPlugin extends SerializerPlugin { |
248 final Resolution resolution; | 249 final Resolution resolution; |
| 250 final Backend backend; |
249 | 251 |
250 ResolvedAstSerializerPlugin(this.resolution); | 252 ResolvedAstSerializerPlugin(this.resolution, this.backend); |
251 | 253 |
252 @override | 254 @override |
253 void onElement(Element element, ObjectEncoder createEncoder(String tag)) { | 255 void onElement(Element element, ObjectEncoder createEncoder(String tag)) { |
254 assert(invariant(element, element.isDeclaration, | 256 assert(invariant(element, element.isDeclaration, |
255 message: "Element $element must be the declaration")); | 257 message: "Element $element must be the declaration")); |
256 if (element is MemberElement) { | 258 if (element is MemberElement) { |
257 assert(invariant(element, resolution.hasResolvedAst(element), | 259 assert(invariant(element, resolution.hasResolvedAst(element), |
258 message: "Element $element must have a resolved ast")); | 260 message: "Element $element must have a resolved ast")); |
259 ResolvedAst resolvedAst = resolution.getResolvedAst(element); | 261 ResolvedAst resolvedAst = resolution.getResolvedAst(element); |
260 ObjectEncoder objectEncoder = createEncoder(RESOLVED_AST_TAG); | 262 ObjectEncoder objectEncoder = createEncoder(RESOLVED_AST_TAG); |
261 new ResolvedAstSerializer(objectEncoder, resolvedAst).serialize(); | 263 new ResolvedAstSerializer( |
| 264 objectEncoder, |
| 265 resolvedAst, |
| 266 backend.serialization.serializer).serialize(); |
262 } | 267 } |
263 } | 268 } |
264 } | 269 } |
265 | 270 |
266 class ResolvedAstDeserializerPlugin extends DeserializerPlugin { | 271 class ResolvedAstDeserializerPlugin extends DeserializerPlugin { |
267 final ParsingContext parsingContext; | 272 final ParsingContext parsingContext; |
| 273 final Backend backend; |
268 final Map<Uri, SourceFile> sourceFiles = <Uri, SourceFile>{}; | 274 final Map<Uri, SourceFile> sourceFiles = <Uri, SourceFile>{}; |
269 | 275 |
270 Map<Element, ResolvedAst> _resolvedAstMap = <Element, ResolvedAst>{}; | 276 Map<Element, ResolvedAst> _resolvedAstMap = <Element, ResolvedAst>{}; |
271 Map<Element, ObjectDecoder> _decoderMap = <Element, ObjectDecoder>{}; | 277 Map<Element, ObjectDecoder> _decoderMap = <Element, ObjectDecoder>{}; |
272 Map<Uri, Token> beginTokenMap = <Uri, Token>{}; | 278 Map<Uri, Token> beginTokenMap = <Uri, Token>{}; |
273 | 279 |
274 ResolvedAstDeserializerPlugin(this.parsingContext); | 280 ResolvedAstDeserializerPlugin(this.parsingContext, this.backend); |
275 | 281 |
276 bool hasResolvedAst(Element element) { | 282 bool hasResolvedAst(Element element) { |
277 return _resolvedAstMap.containsKey(element) || | 283 return _resolvedAstMap.containsKey(element) || |
278 _decoderMap.containsKey(element); | 284 _decoderMap.containsKey(element); |
279 } | 285 } |
280 | 286 |
281 ResolvedAst getResolvedAst(Element element) { | 287 ResolvedAst getResolvedAst(Element element) { |
282 ResolvedAst resolvedAst = _resolvedAstMap[element]; | 288 ResolvedAst resolvedAst = _resolvedAstMap[element]; |
283 if (resolvedAst == null) { | 289 if (resolvedAst == null) { |
284 ObjectDecoder decoder = _decoderMap[element]; | 290 ObjectDecoder decoder = _decoderMap[element]; |
285 if (decoder != null) { | 291 if (decoder != null) { |
286 resolvedAst = _resolvedAstMap[element] = | 292 resolvedAst = _resolvedAstMap[element] = |
287 ResolvedAstDeserializer.deserialize( | 293 ResolvedAstDeserializer.deserialize( |
288 element, decoder, parsingContext, findToken); | 294 element, decoder, parsingContext, findToken, |
| 295 backend.serialization.deserializer); |
289 _decoderMap.remove(element); | 296 _decoderMap.remove(element); |
290 } | 297 } |
291 } | 298 } |
292 return resolvedAst; | 299 return resolvedAst; |
293 } | 300 } |
294 | 301 |
295 Token findToken(Uri uri, int offset) { | 302 Token findToken(Uri uri, int offset) { |
296 Token beginToken = beginTokenMap.putIfAbsent(uri, () { | 303 Token beginToken = beginTokenMap.putIfAbsent(uri, () { |
297 SourceFile sourceFile = sourceFiles[uri]; | 304 SourceFile sourceFile = sourceFiles[uri]; |
298 if (sourceFile == null) { | 305 if (sourceFile == null) { |
299 throw 'No source file found for $uri in:\n ' | 306 throw 'No source file found for $uri in:\n ' |
300 '${sourceFiles.keys.join('\n ')}'; | 307 '${sourceFiles.keys.join('\n ')}'; |
301 } | 308 } |
302 return new Scanner(sourceFile).tokenize(); | 309 return new Scanner(sourceFile).tokenize(); |
303 }); | 310 }); |
304 return ResolvedAstDeserializer.findTokenInStream(beginToken, offset); | 311 return ResolvedAstDeserializer.findTokenInStream(beginToken, offset); |
305 } | 312 } |
306 | 313 |
307 @override | 314 @override |
308 void onElement(Element element, ObjectDecoder getDecoder(String tag)) { | 315 void onElement(Element element, ObjectDecoder getDecoder(String tag)) { |
309 ObjectDecoder decoder = getDecoder(RESOLVED_AST_TAG); | 316 ObjectDecoder decoder = getDecoder(RESOLVED_AST_TAG); |
310 if (decoder != null) { | 317 if (decoder != null) { |
311 _decoderMap[element] = decoder; | 318 _decoderMap[element] = decoder; |
312 } | 319 } |
313 } | 320 } |
314 } | 321 } |
315 | 322 |
OLD | NEW |