| 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 '../common.dart'; | 7 import '../common.dart'; |
| 8 import '../constants/constructors.dart'; | 8 import '../constants/constructors.dart'; |
| 9 import '../constants/expressions.dart'; | 9 import '../constants/expressions.dart'; |
| 10 import '../dart_types.dart'; | 10 import '../dart_types.dart'; |
| 11 import '../elements/elements.dart'; | 11 import '../elements/elements.dart'; |
| 12 import 'constant_serialization.dart'; | 12 import 'constant_serialization.dart'; |
| 13 import 'keys.dart'; | 13 import 'keys.dart'; |
| 14 import 'modelz.dart'; | 14 import 'modelz.dart'; |
| 15 import 'serialization.dart'; | 15 import 'serialization.dart'; |
| 16 | 16 |
| 17 /// Enum kinds used for encoding [Element]s. | 17 /// Enum kinds used for encoding [Element]s. |
| 18 enum SerializedElementKind { | 18 enum SerializedElementKind { |
| 19 LIBRARY, | 19 LIBRARY, |
| 20 COMPILATION_UNIT, | 20 COMPILATION_UNIT, |
| 21 CLASS, | 21 CLASS, |
| 22 ENUM, | |
| 23 GENERATIVE_CONSTRUCTOR, | 22 GENERATIVE_CONSTRUCTOR, |
| 24 FACTORY_CONSTRUCTOR, | 23 FACTORY_CONSTRUCTOR, |
| 25 TOPLEVEL_FIELD, | 24 TOPLEVEL_FIELD, |
| 26 STATIC_FIELD, | 25 STATIC_FIELD, |
| 27 INSTANCE_FIELD, | 26 INSTANCE_FIELD, |
| 28 TOPLEVEL_FUNCTION, | 27 TOPLEVEL_FUNCTION, |
| 29 TOPLEVEL_GETTER, | 28 TOPLEVEL_GETTER, |
| 30 TOPLEVEL_SETTER, | 29 TOPLEVEL_SETTER, |
| 31 STATIC_FUNCTION, | 30 STATIC_FUNCTION, |
| 32 STATIC_GETTER, | 31 STATIC_GETTER, |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 | 109 |
| 111 /// Serialize the parameters of [element] into [encoder]. | 110 /// Serialize the parameters of [element] into [encoder]. |
| 112 static void serializeParameters(FunctionElement element, | 111 static void serializeParameters(FunctionElement element, |
| 113 ObjectEncoder encoder) { | 112 ObjectEncoder encoder) { |
| 114 FunctionType type = element.type; | 113 FunctionType type = element.type; |
| 115 encoder.setType(Key.RETURN_TYPE, type.returnType); | 114 encoder.setType(Key.RETURN_TYPE, type.returnType); |
| 116 encoder.setElements(Key.PARAMETERS, element.parameters); | 115 encoder.setElements(Key.PARAMETERS, element.parameters); |
| 117 } | 116 } |
| 118 | 117 |
| 119 /// Returns a function that adds the underlying declared elements for a | 118 /// Returns a function that adds the underlying declared elements for a |
| 120 /// particular element into [set]. | 119 /// particular element into [list]. |
| 121 /// | 120 /// |
| 122 /// For instance, for an [AbstractFieldElement] the getter and setter elements | 121 /// For instance, for an [AbstractFieldElement] the getter and setter elements |
| 123 /// are added, if available. | 122 /// are added, if available. |
| 124 static flattenElements(Set<Element> set) { | 123 static flattenElements(List<Element> list) { |
| 125 return (Element element) { | 124 return (Element element) { |
| 126 if (element.isPatch) return; | |
| 127 // TODO(johnniwinther): Handle ambiguous elements. | 125 // TODO(johnniwinther): Handle ambiguous elements. |
| 128 if (element.isAmbiguous) return; | 126 if (element.isAmbiguous) return; |
| 129 if (element.isAbstractField) { | 127 if (element.isAbstractField) { |
| 130 AbstractFieldElement abstractField = element; | 128 AbstractFieldElement abstractField = element; |
| 131 if (abstractField.getter != null) { | 129 if (abstractField.getter != null) { |
| 132 set.add(abstractField.getter); | 130 list.add(abstractField.getter); |
| 133 } | 131 } |
| 134 if (abstractField.setter != null) { | 132 if (abstractField.setter != null) { |
| 135 set.add(abstractField.setter); | 133 list.add(abstractField.setter); |
| 136 } | 134 } |
| 137 } else { | 135 } else { |
| 138 set.add(element); | 136 list.add(element); |
| 139 } | 137 } |
| 140 }; | 138 }; |
| 141 } | 139 } |
| 142 } | 140 } |
| 143 | 141 |
| 144 class LibrarySerializer implements ElementSerializer { | 142 class LibrarySerializer implements ElementSerializer { |
| 145 const LibrarySerializer(); | 143 const LibrarySerializer(); |
| 146 | 144 |
| 147 SerializedElementKind getSerializedKind(Element element) { | 145 SerializedElementKind getSerializedKind(Element element) { |
| 148 if (element.isLibrary) { | 146 if (element.isLibrary) { |
| 149 return SerializedElementKind.LIBRARY; | 147 return SerializedElementKind.LIBRARY; |
| 150 } | 148 } |
| 151 return null; | 149 return null; |
| 152 } | 150 } |
| 153 | 151 |
| 154 static List<CompilationUnitElement> getCompilationUnits( | |
| 155 LibraryElement element) { | |
| 156 List<CompilationUnitElement> compilationUnits = <CompilationUnitElement>[]; | |
| 157 compilationUnits.addAll(element.compilationUnits.toList()); | |
| 158 if (element.isPatched) { | |
| 159 compilationUnits.addAll(element.implementation.compilationUnits.toList()); | |
| 160 } | |
| 161 return compilationUnits; | |
| 162 } | |
| 163 | |
| 164 static List<ImportElement> getImports(LibraryElement element) { | |
| 165 List<ImportElement> imports = <ImportElement>[]; | |
| 166 imports.addAll(element.imports); | |
| 167 if (element.isPatched) { | |
| 168 imports.addAll(element.implementation.imports); | |
| 169 } | |
| 170 return imports; | |
| 171 } | |
| 172 | |
| 173 static List<Element> getImportedElements( | |
| 174 LibraryElement element) { | |
| 175 Set<Element> importedElements = new Set<Element>(); | |
| 176 element.forEachImport(SerializerUtil.flattenElements(importedElements)); | |
| 177 if (element.isPatched) { | |
| 178 element.implementation.forEachImport( | |
| 179 SerializerUtil.flattenElements(importedElements)); | |
| 180 } | |
| 181 return importedElements.toList(); | |
| 182 } | |
| 183 | |
| 184 static List<Element> getExportedElements( | |
| 185 LibraryElement element) { | |
| 186 Set<Element> exportedElements = new Set<Element>(); | |
| 187 element.forEachExport(SerializerUtil.flattenElements(exportedElements)); | |
| 188 return exportedElements.toList(); | |
| 189 } | |
| 190 | |
| 191 void serialize(LibraryElement element, | 152 void serialize(LibraryElement element, |
| 192 ObjectEncoder encoder, | 153 ObjectEncoder encoder, |
| 193 SerializedElementKind kind) { | 154 SerializedElementKind kind) { |
| 194 encoder.setUri( | 155 encoder.setUri( |
| 195 Key.CANONICAL_URI, element.canonicalUri, element.canonicalUri); | 156 Key.CANONICAL_URI, element.canonicalUri, element.canonicalUri); |
| 196 encoder.setString(Key.LIBRARY_NAME, element.libraryName); | 157 encoder.setString(Key.LIBRARY_NAME, element.libraryName); |
| 197 SerializerUtil.serializeMembers(element, encoder); | 158 SerializerUtil.serializeMembers(element, encoder); |
| 198 encoder.setElement(Key.COMPILATION_UNIT, element.entryCompilationUnit); | 159 encoder.setElement(Key.COMPILATION_UNIT, element.entryCompilationUnit); |
| 199 encoder.setElements(Key.COMPILATION_UNITS, getCompilationUnits(element)); | 160 encoder.setElements( |
| 200 encoder.setElements(Key.IMPORTS, getImports(element)); | 161 Key.COMPILATION_UNITS, element.compilationUnits.toList()); |
| 162 encoder.setElements(Key.IMPORTS, element.imports); |
| 201 encoder.setElements(Key.EXPORTS, element.exports); | 163 encoder.setElements(Key.EXPORTS, element.exports); |
| 202 | 164 |
| 203 encoder.setElements(Key.IMPORT_SCOPE, getImportedElements(element)); | 165 List<Element> importedElements = <Element>[]; |
| 166 element.forEachImport(SerializerUtil.flattenElements(importedElements)); |
| 167 encoder.setElements(Key.IMPORT_SCOPE, importedElements); |
| 204 | 168 |
| 205 encoder.setElements(Key.EXPORT_SCOPE, getExportedElements(element)); | 169 List<Element> exportedElements = <Element>[]; |
| 170 element.forEachExport(SerializerUtil.flattenElements(exportedElements)); |
| 171 encoder.setElements(Key.EXPORT_SCOPE, exportedElements); |
| 206 | 172 |
| 207 } | 173 } |
| 208 } | 174 } |
| 209 | 175 |
| 210 class CompilationUnitSerializer implements ElementSerializer { | 176 class CompilationUnitSerializer implements ElementSerializer { |
| 211 const CompilationUnitSerializer(); | 177 const CompilationUnitSerializer(); |
| 212 | 178 |
| 213 SerializedElementKind getSerializedKind(Element element) { | 179 SerializedElementKind getSerializedKind(Element element) { |
| 214 if (element.isCompilationUnit) { | 180 if (element.isCompilationUnit) { |
| 215 return SerializedElementKind.COMPILATION_UNIT; | 181 return SerializedElementKind.COMPILATION_UNIT; |
| 216 } | 182 } |
| 217 return null; | 183 return null; |
| 218 } | 184 } |
| 219 | 185 |
| 220 void serialize(CompilationUnitElement element, | 186 void serialize(CompilationUnitElement element, |
| 221 ObjectEncoder encoder, | 187 ObjectEncoder encoder, |
| 222 SerializedElementKind kind) { | 188 SerializedElementKind kind) { |
| 223 encoder.setElement(Key.LIBRARY, element.library); | 189 encoder.setElement(Key.LIBRARY, element.library); |
| 224 encoder.setUri( | 190 encoder.setUri( |
| 225 Key.URI, element.library.canonicalUri, element.script.resourceUri); | 191 Key.URI, element.library.canonicalUri, element.script.resourceUri); |
| 226 List<Element> elements = <Element>[]; | 192 List<Element> elements = <Element>[]; |
| 227 element.forEachLocalMember((e) { | 193 element.forEachLocalMember((e) => elements.add(e)); |
| 228 if (!element.isPatch) { | |
| 229 elements.add(e); | |
| 230 } | |
| 231 }); | |
| 232 encoder.setElements(Key.ELEMENTS, elements); | 194 encoder.setElements(Key.ELEMENTS, elements); |
| 233 } | 195 } |
| 234 } | 196 } |
| 235 | 197 |
| 236 class ClassSerializer implements ElementSerializer { | 198 class ClassSerializer implements ElementSerializer { |
| 237 const ClassSerializer(); | 199 const ClassSerializer(); |
| 238 | 200 |
| 239 SerializedElementKind getSerializedKind(Element element) { | 201 SerializedElementKind getSerializedKind(Element element) { |
| 240 if (element.isClass) { | 202 if (element.isClass) { |
| 241 ClassElement cls = element; | |
| 242 if (cls.isEnumClass) { | |
| 243 return SerializedElementKind.ENUM; | |
| 244 } | |
| 245 return SerializedElementKind.CLASS; | 203 return SerializedElementKind.CLASS; |
| 246 } | 204 } |
| 247 return null; | 205 return null; |
| 248 } | 206 } |
| 249 | 207 |
| 250 void serialize(ClassElement element, | 208 void serialize(ClassElement element, |
| 251 ObjectEncoder encoder, | 209 ObjectEncoder encoder, |
| 252 SerializedElementKind kind) { | 210 SerializedElementKind kind) { |
| 253 encoder.setElement(Key.LIBRARY, element.library); | 211 encoder.setElement(Key.LIBRARY, element.library); |
| 254 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); | 212 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); |
| 255 encoder.setString(Key.NAME, element.name); | 213 encoder.setString(Key.NAME, element.name); |
| 256 SerializerUtil.serializePosition(element, encoder); | 214 SerializerUtil.serializePosition(element, encoder); |
| 257 encoder.setTypes(Key.TYPE_VARIABLES, element.typeVariables); | 215 encoder.setTypes(Key.TYPE_VARIABLES, element.typeVariables); |
| 258 encoder.setBool(Key.IS_ABSTRACT, element.isAbstract); | 216 encoder.setBool(Key.IS_ABSTRACT, element.isAbstract); |
| 259 if (element.supertype != null) { | 217 if (element.supertype != null) { |
| 260 encoder.setType(Key.SUPERTYPE, element.supertype); | 218 encoder.setType(Key.SUPERTYPE, element.supertype); |
| 261 } | 219 } |
| 262 // TODO(johnniwinther): Make [OrderedTypeSet] easier to (de)serialize. | 220 // TODO(johnniwinther): Make [OrderedTypeSet] easier to (de)serialize. |
| 263 ObjectEncoder supertypes = encoder.createObject(Key.SUPERTYPES); | 221 ObjectEncoder supertypes = encoder.createObject(Key.SUPERTYPES); |
| 264 supertypes.setTypes(Key.TYPES, | 222 supertypes.setTypes(Key.TYPES, |
| 265 element.allSupertypesAndSelf.types.toList()); | 223 element.allSupertypesAndSelf.types.toList()); |
| 266 supertypes.setTypes(Key.SUPERTYPES, | 224 supertypes.setTypes(Key.SUPERTYPES, |
| 267 element.allSupertypesAndSelf.supertypes.toList()); | 225 element.allSupertypesAndSelf.supertypes.toList()); |
| 268 supertypes.setInts(Key.OFFSETS, element.allSupertypesAndSelf.levelOffsets); | 226 supertypes.setInts(Key.OFFSETS, element.allSupertypesAndSelf.levelOffsets); |
| 269 encoder.setTypes(Key.INTERFACES, element.interfaces.toList()); | 227 encoder.setTypes(Key.INTERFACES, element.interfaces.toList()); |
| 270 SerializerUtil.serializeMembers(element, encoder); | 228 SerializerUtil.serializeMembers(element, encoder); |
| 271 if (kind == SerializedElementKind.ENUM) { | |
| 272 EnumClassElement enumClass = element; | |
| 273 encoder.setElements(Key.FIELDS, enumClass.enumValues); | |
| 274 } | |
| 275 } | 229 } |
| 276 } | 230 } |
| 277 | 231 |
| 278 class ConstructorSerializer implements ElementSerializer { | 232 class ConstructorSerializer implements ElementSerializer { |
| 279 const ConstructorSerializer(); | 233 const ConstructorSerializer(); |
| 280 | 234 |
| 281 SerializedElementKind getSerializedKind(Element element) { | 235 SerializedElementKind getSerializedKind(Element element) { |
| 282 if (element.isGenerativeConstructor) { | 236 if (element.isGenerativeConstructor) { |
| 283 return SerializedElementKind.GENERATIVE_CONSTRUCTOR; | 237 return SerializedElementKind.GENERATIVE_CONSTRUCTOR; |
| 284 } else if (element.isFactoryConstructor) { | 238 } else if (element.isFactoryConstructor) { |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 encoder.setType(Key.TYPE, element.type); | 333 encoder.setType(Key.TYPE, element.type); |
| 380 if (element.isFunction) { | 334 if (element.isFunction) { |
| 381 encoder.setBool(Key.IS_OPERATOR, element.isOperator); | 335 encoder.setBool(Key.IS_OPERATOR, element.isOperator); |
| 382 } | 336 } |
| 383 if (element.enclosingClass != null) { | 337 if (element.enclosingClass != null) { |
| 384 encoder.setElement(Key.CLASS, element.enclosingClass); | 338 encoder.setElement(Key.CLASS, element.enclosingClass); |
| 385 } else { | 339 } else { |
| 386 encoder.setElement(Key.LIBRARY, element.library); | 340 encoder.setElement(Key.LIBRARY, element.library); |
| 387 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); | 341 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); |
| 388 } | 342 } |
| 389 encoder.setBool(Key.IS_EXTERNAL, element.isExternal); | |
| 390 } | 343 } |
| 391 } | 344 } |
| 392 | 345 |
| 393 class TypedefSerializer implements ElementSerializer { | 346 class TypedefSerializer implements ElementSerializer { |
| 394 const TypedefSerializer(); | 347 const TypedefSerializer(); |
| 395 | 348 |
| 396 SerializedElementKind getSerializedKind(Element element) { | 349 SerializedElementKind getSerializedKind(Element element) { |
| 397 if (element.isTypedef) { | 350 if (element.isTypedef) { |
| 398 return SerializedElementKind.TYPEDEF; | 351 return SerializedElementKind.TYPEDEF; |
| 399 } | 352 } |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 548 static Element deserialize(ObjectDecoder decoder) { | 501 static Element deserialize(ObjectDecoder decoder) { |
| 549 SerializedElementKind elementKind = | 502 SerializedElementKind elementKind = |
| 550 decoder.getEnum(Key.KIND, SerializedElementKind.values); | 503 decoder.getEnum(Key.KIND, SerializedElementKind.values); |
| 551 switch (elementKind) { | 504 switch (elementKind) { |
| 552 case SerializedElementKind.LIBRARY: | 505 case SerializedElementKind.LIBRARY: |
| 553 return new LibraryElementZ(decoder); | 506 return new LibraryElementZ(decoder); |
| 554 case SerializedElementKind.COMPILATION_UNIT: | 507 case SerializedElementKind.COMPILATION_UNIT: |
| 555 return new CompilationUnitElementZ(decoder); | 508 return new CompilationUnitElementZ(decoder); |
| 556 case SerializedElementKind.CLASS: | 509 case SerializedElementKind.CLASS: |
| 557 return new ClassElementZ(decoder); | 510 return new ClassElementZ(decoder); |
| 558 case SerializedElementKind.ENUM: | |
| 559 return new EnumClassElementZ(decoder); | |
| 560 case SerializedElementKind.TOPLEVEL_FIELD: | 511 case SerializedElementKind.TOPLEVEL_FIELD: |
| 561 return new TopLevelFieldElementZ(decoder); | 512 return new TopLevelFieldElementZ(decoder); |
| 562 case SerializedElementKind.STATIC_FIELD: | 513 case SerializedElementKind.STATIC_FIELD: |
| 563 return new StaticFieldElementZ(decoder); | 514 return new StaticFieldElementZ(decoder); |
| 564 case SerializedElementKind.INSTANCE_FIELD: | 515 case SerializedElementKind.INSTANCE_FIELD: |
| 565 return new InstanceFieldElementZ(decoder); | 516 return new InstanceFieldElementZ(decoder); |
| 566 case SerializedElementKind.GENERATIVE_CONSTRUCTOR: | 517 case SerializedElementKind.GENERATIVE_CONSTRUCTOR: |
| 567 return new GenerativeConstructorElementZ(decoder); | 518 return new GenerativeConstructorElementZ(decoder); |
| 568 case SerializedElementKind.FACTORY_CONSTRUCTOR: | 519 case SerializedElementKind.FACTORY_CONSTRUCTOR: |
| 569 return new FactoryConstructorElementZ(decoder); | 520 return new FactoryConstructorElementZ(decoder); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 596 case SerializedElementKind.IMPORT: | 547 case SerializedElementKind.IMPORT: |
| 597 return new ImportElementZ(decoder); | 548 return new ImportElementZ(decoder); |
| 598 case SerializedElementKind.EXPORT: | 549 case SerializedElementKind.EXPORT: |
| 599 return new ExportElementZ(decoder); | 550 return new ExportElementZ(decoder); |
| 600 case SerializedElementKind.PREFIX: | 551 case SerializedElementKind.PREFIX: |
| 601 return new PrefixElementZ(decoder); | 552 return new PrefixElementZ(decoder); |
| 602 } | 553 } |
| 603 throw new UnsupportedError("Unexpected element kind '${elementKind}."); | 554 throw new UnsupportedError("Unexpected element kind '${elementKind}."); |
| 604 } | 555 } |
| 605 } | 556 } |
| OLD | NEW |