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