| 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 dart2js.serialization.elements; | 5 library dart2js.serialization.elements; |
| 6 | 6 |
| 7 import '../constants/constructors.dart'; | 7 import '../constants/constructors.dart'; |
| 8 import '../constants/expressions.dart'; | 8 import '../constants/expressions.dart'; |
| 9 import '../dart_types.dart'; | 9 import '../dart_types.dart'; |
| 10 import '../diagnostics/source_span.dart' show | 10 import '../diagnostics/source_span.dart' show |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 STATIC_FUNCTION, | 32 STATIC_FUNCTION, |
| 33 STATIC_GETTER, | 33 STATIC_GETTER, |
| 34 STATIC_SETTER, | 34 STATIC_SETTER, |
| 35 INSTANCE_FUNCTION, | 35 INSTANCE_FUNCTION, |
| 36 INSTANCE_GETTER, | 36 INSTANCE_GETTER, |
| 37 INSTANCE_SETTER, | 37 INSTANCE_SETTER, |
| 38 TYPEDEF, | 38 TYPEDEF, |
| 39 TYPEVARIABLE, | 39 TYPEVARIABLE, |
| 40 PARAMETER, | 40 PARAMETER, |
| 41 INITIALIZING_FORMAL, | 41 INITIALIZING_FORMAL, |
| 42 IMPORT, |
| 43 EXPORT, |
| 44 PREFIX, |
| 42 } | 45 } |
| 43 | 46 |
| 44 /// Set of serializers used to serialize different kinds of elements by | 47 /// Set of serializers used to serialize different kinds of elements by |
| 45 /// encoding into them into [ObjectEncoder]s. | 48 /// encoding into them into [ObjectEncoder]s. |
| 46 /// | 49 /// |
| 47 /// This class is called from the [Serializer] when an [Element] needs | 50 /// This class is called from the [Serializer] when an [Element] needs |
| 48 /// serialization. The [ObjectEncoder] ensures that any [Element], [DartType], | 51 /// serialization. The [ObjectEncoder] ensures that any [Element], [DartType], |
| 49 /// and [ConstantExpression] that the serialized [Element] depends upon are also | 52 /// and [ConstantExpression] that the serialized [Element] depends upon are also |
| 50 /// serialized. | 53 /// serialized. |
| 51 const List<ElementSerializer> ELEMENT_SERIALIZERS = const [ | 54 const List<ElementSerializer> ELEMENT_SERIALIZERS = const [ |
| 52 const LibrarySerializer(), | 55 const LibrarySerializer(), |
| 53 const CompilationUnitSerializer(), | 56 const CompilationUnitSerializer(), |
| 54 const ClassSerializer(), | 57 const ClassSerializer(), |
| 55 const ConstructorSerializer(), | 58 const ConstructorSerializer(), |
| 56 const FieldSerializer(), | 59 const FieldSerializer(), |
| 57 const FunctionSerializer(), | 60 const FunctionSerializer(), |
| 58 const TypedefSerializer(), | 61 const TypedefSerializer(), |
| 59 const TypeVariableSerializer(), | 62 const TypeVariableSerializer(), |
| 60 const ParameterSerializer(), | 63 const ParameterSerializer(), |
| 64 const ImportSerializer(), |
| 65 const ExportSerializer(), |
| 66 const PrefixSerializer(), |
| 61 ]; | 67 ]; |
| 62 | 68 |
| 63 /// Interface for a function that can serialize a set of element kinds. | 69 /// Interface for a function that can serialize a set of element kinds. |
| 64 abstract class ElementSerializer { | 70 abstract class ElementSerializer { |
| 65 /// Returns the [SerializedElementKind] for [element] if this serializer | 71 /// Returns the [SerializedElementKind] for [element] if this serializer |
| 66 /// supports serialization of [element] or `null` otherwise. | 72 /// supports serialization of [element] or `null` otherwise. |
| 67 SerializedElementKind getSerializedKind(Element element); | 73 SerializedElementKind getSerializedKind(Element element); |
| 68 | 74 |
| 69 /// Serializes [element] into the [encoder] using the [kind] computed | 75 /// Serializes [element] into the [encoder] using the [kind] computed |
| 70 /// by [getSerializedKind]. | 76 /// by [getSerializedKind]. |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 return SerializedElementKind.LIBRARY; | 149 return SerializedElementKind.LIBRARY; |
| 144 } | 150 } |
| 145 return null; | 151 return null; |
| 146 } | 152 } |
| 147 | 153 |
| 148 void serialize(LibraryElement element, | 154 void serialize(LibraryElement element, |
| 149 ObjectEncoder encoder, | 155 ObjectEncoder encoder, |
| 150 SerializedElementKind kind) { | 156 SerializedElementKind kind) { |
| 151 encoder.setUri( | 157 encoder.setUri( |
| 152 Key.CANONICAL_URI, element.canonicalUri, element.canonicalUri); | 158 Key.CANONICAL_URI, element.canonicalUri, element.canonicalUri); |
| 153 encoder.setString(Key.LIBRARY_NAME, element.getLibraryName()); | 159 encoder.setString(Key.LIBRARY_NAME, element.libraryName); |
| 154 SerializerUtil.serializeMembers(element, encoder); | 160 SerializerUtil.serializeMembers(element, encoder); |
| 155 encoder.setElement(Key.COMPILATION_UNIT, element.entryCompilationUnit); | 161 encoder.setElement(Key.COMPILATION_UNIT, element.entryCompilationUnit); |
| 156 encoder.setElements( | 162 encoder.setElements( |
| 157 Key.COMPILATION_UNITS, element.compilationUnits.toList()); | 163 Key.COMPILATION_UNITS, element.compilationUnits.toList()); |
| 158 ListEncoder tags = encoder.createList(Key.TAGS); | 164 encoder.setElements(Key.IMPORTS, element.imports); |
| 165 encoder.setElements(Key.EXPORTS, element.exports); |
| 159 | 166 |
| 160 for (LibraryTag tag in element.tags) { | 167 List<Element> importedElements = <Element>[]; |
| 161 if (tag is Import) { | 168 element.forEachImport(SerializerUtil.flattenElements(importedElements)); |
| 162 ObjectEncoder importTag = tags.createObject(); | 169 encoder.setElements(Key.IMPORT_SCOPE, importedElements); |
| 163 importTag.setString(Key.KIND, 'import'); | |
| 164 importTag.setElement(Key.LIBRARY, element.getLibraryFromTag(tag)); | |
| 165 } else if (tag is Export) { | |
| 166 ObjectEncoder exportTag = tags.createObject(); | |
| 167 exportTag.setString(Key.KIND, 'export'); | |
| 168 exportTag.setElement(Key.LIBRARY, element.getLibraryFromTag(tag)); | |
| 169 } | |
| 170 } | |
| 171 | 170 |
| 172 List<Element> imports = <Element>[]; | 171 List<Element> exportedElements = <Element>[]; |
| 173 element.forEachImport(SerializerUtil.flattenElements(imports)); | 172 element.forEachExport(SerializerUtil.flattenElements(exportedElements)); |
| 174 encoder.setElements(Key.IMPORTS, imports); | 173 encoder.setElements(Key.EXPORT_SCOPE, exportedElements); |
| 175 | 174 |
| 176 List<Element> exports = <Element>[]; | |
| 177 element.forEachExport(SerializerUtil.flattenElements(exports)); | |
| 178 encoder.setElements(Key.EXPORTS, exports); | |
| 179 } | 175 } |
| 180 } | 176 } |
| 181 | 177 |
| 182 class CompilationUnitSerializer implements ElementSerializer { | 178 class CompilationUnitSerializer implements ElementSerializer { |
| 183 const CompilationUnitSerializer(); | 179 const CompilationUnitSerializer(); |
| 184 | 180 |
| 185 SerializedElementKind getSerializedKind(Element element) { | 181 SerializedElementKind getSerializedKind(Element element) { |
| 186 if (element.isCompilationUnit) { | 182 if (element.isCompilationUnit) { |
| 187 return SerializedElementKind.COMPILATION_UNIT; | 183 return SerializedElementKind.COMPILATION_UNIT; |
| 188 } | 184 } |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 if (element.isOptional) { | 414 if (element.isOptional) { |
| 419 encoder.setConstant(Key.CONSTANT, element.constant); | 415 encoder.setConstant(Key.CONSTANT, element.constant); |
| 420 } | 416 } |
| 421 if (element.isInitializingFormal) { | 417 if (element.isInitializingFormal) { |
| 422 InitializingFormalElement initializingFormal = element; | 418 InitializingFormalElement initializingFormal = element; |
| 423 encoder.setElement(Key.FIELD, initializingFormal.fieldElement); | 419 encoder.setElement(Key.FIELD, initializingFormal.fieldElement); |
| 424 } | 420 } |
| 425 } | 421 } |
| 426 } | 422 } |
| 427 | 423 |
| 424 class ImportSerializer implements ElementSerializer { |
| 425 const ImportSerializer(); |
| 426 |
| 427 SerializedElementKind getSerializedKind(Element element) { |
| 428 if (element.isImport) { |
| 429 return SerializedElementKind.IMPORT; |
| 430 } |
| 431 return null; |
| 432 } |
| 433 |
| 434 void serialize(ImportElement element, |
| 435 ObjectEncoder encoder, |
| 436 SerializedElementKind kind) { |
| 437 encoder.setElement(Key.LIBRARY, element.library); |
| 438 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); |
| 439 encoder.setElement(Key.LIBRARY_DEPENDENCY, element.importedLibrary); |
| 440 if (element.prefix != null) { |
| 441 encoder.setElement(Key.PREFIX, element.prefix); |
| 442 } |
| 443 encoder.setBool(Key.IS_DEFERRED, element.isDeferred); |
| 444 // TODO(johnniwinther): What is the base for the URI? |
| 445 encoder.setUri(Key.URI, element.uri, element.uri); |
| 446 } |
| 447 } |
| 448 |
| 449 class ExportSerializer implements ElementSerializer { |
| 450 const ExportSerializer(); |
| 451 |
| 452 SerializedElementKind getSerializedKind(Element element) { |
| 453 if (element.isExport) { |
| 454 return SerializedElementKind.EXPORT; |
| 455 } |
| 456 return null; |
| 457 } |
| 458 |
| 459 void serialize(ExportElement element, |
| 460 ObjectEncoder encoder, |
| 461 SerializedElementKind kind) { |
| 462 encoder.setElement(Key.LIBRARY, element.library); |
| 463 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); |
| 464 encoder.setElement(Key.LIBRARY_DEPENDENCY, element.exportedLibrary); |
| 465 // TODO(johnniwinther): What is the base for the URI? |
| 466 encoder.setUri(Key.URI, element.uri, element.uri); |
| 467 } |
| 468 } |
| 469 |
| 470 class PrefixSerializer implements ElementSerializer { |
| 471 const PrefixSerializer(); |
| 472 |
| 473 SerializedElementKind getSerializedKind(Element element) { |
| 474 if (element.isPrefix) { |
| 475 return SerializedElementKind.PREFIX; |
| 476 } |
| 477 return null; |
| 478 } |
| 479 |
| 480 void serialize(PrefixElement element, |
| 481 ObjectEncoder encoder, |
| 482 SerializedElementKind kind) { |
| 483 encoder.setString(Key.NAME, element.name); |
| 484 encoder.setElement(Key.LIBRARY, element.library); |
| 485 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); |
| 486 if (element.deferredImport != null) { |
| 487 encoder.setElement(Key.IMPORT, element.deferredImport); |
| 488 } |
| 489 encoder.setBool(Key.IS_DEFERRED, element.isDeferred); |
| 490 } |
| 491 } |
| 492 |
| 428 /// Utility class for deserializing [Element]s. | 493 /// Utility class for deserializing [Element]s. |
| 429 /// | 494 /// |
| 430 /// This is used by the [Deserializer]. | 495 /// This is used by the [Deserializer]. |
| 431 class ElementDeserializer { | 496 class ElementDeserializer { |
| 432 | 497 |
| 433 /// Deserializes an [Element] from an [ObjectDecoder]. | 498 /// Deserializes an [Element] from an [ObjectDecoder]. |
| 434 /// | 499 /// |
| 435 /// The class is called from the [Deserializer] when an [Element] | 500 /// The class is called from the [Deserializer] when an [Element] |
| 436 /// needs deserialization. The [ObjectDecoder] ensures that any [Element], | 501 /// needs deserialization. The [ObjectDecoder] ensures that any [Element], |
| 437 /// [DartType], and [ConstantExpression] that the deserialized [Element] | 502 /// [DartType], and [ConstantExpression] that the deserialized [Element] |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 case SerializedElementKind.INSTANCE_SETTER: | 540 case SerializedElementKind.INSTANCE_SETTER: |
| 476 return new InstanceSetterElementZ(decoder); | 541 return new InstanceSetterElementZ(decoder); |
| 477 case SerializedElementKind.TYPEDEF: | 542 case SerializedElementKind.TYPEDEF: |
| 478 return new TypedefElementZ(decoder); | 543 return new TypedefElementZ(decoder); |
| 479 case SerializedElementKind.TYPEVARIABLE: | 544 case SerializedElementKind.TYPEVARIABLE: |
| 480 return new TypeVariableElementZ(decoder); | 545 return new TypeVariableElementZ(decoder); |
| 481 case SerializedElementKind.PARAMETER: | 546 case SerializedElementKind.PARAMETER: |
| 482 return new ParameterElementZ(decoder); | 547 return new ParameterElementZ(decoder); |
| 483 case SerializedElementKind.INITIALIZING_FORMAL: | 548 case SerializedElementKind.INITIALIZING_FORMAL: |
| 484 return new InitializingFormalElementZ(decoder); | 549 return new InitializingFormalElementZ(decoder); |
| 550 case SerializedElementKind.IMPORT: |
| 551 return new ImportElementZ(decoder); |
| 552 case SerializedElementKind.EXPORT: |
| 553 return new ExportElementZ(decoder); |
| 554 case SerializedElementKind.PREFIX: |
| 555 return new PrefixElementZ(decoder); |
| 485 } | 556 } |
| 486 throw new UnsupportedError("Unexpected element kind '${elementKind}."); | 557 throw new UnsupportedError("Unexpected element kind '${elementKind}."); |
| 487 } | 558 } |
| 488 } | 559 } |
| OLD | NEW |