| 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 ENUM, |
| 23 NAMED_MIXIN_APPLICATION, |
| 23 GENERATIVE_CONSTRUCTOR, | 24 GENERATIVE_CONSTRUCTOR, |
| 24 FACTORY_CONSTRUCTOR, | 25 FACTORY_CONSTRUCTOR, |
| 25 TOPLEVEL_FIELD, | 26 TOPLEVEL_FIELD, |
| 26 STATIC_FIELD, | 27 STATIC_FIELD, |
| 27 INSTANCE_FIELD, | 28 INSTANCE_FIELD, |
| 28 TOPLEVEL_FUNCTION, | 29 TOPLEVEL_FUNCTION, |
| 29 TOPLEVEL_GETTER, | 30 TOPLEVEL_GETTER, |
| 30 TOPLEVEL_SETTER, | 31 TOPLEVEL_SETTER, |
| 31 STATIC_FUNCTION, | 32 STATIC_FUNCTION, |
| 32 STATIC_GETTER, | 33 STATIC_GETTER, |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 } | 246 } |
| 246 | 247 |
| 247 class ClassSerializer implements ElementSerializer { | 248 class ClassSerializer implements ElementSerializer { |
| 248 const ClassSerializer(); | 249 const ClassSerializer(); |
| 249 | 250 |
| 250 SerializedElementKind getSerializedKind(Element element) { | 251 SerializedElementKind getSerializedKind(Element element) { |
| 251 if (element.isClass) { | 252 if (element.isClass) { |
| 252 ClassElement cls = element; | 253 ClassElement cls = element; |
| 253 if (cls.isEnumClass) { | 254 if (cls.isEnumClass) { |
| 254 return SerializedElementKind.ENUM; | 255 return SerializedElementKind.ENUM; |
| 256 } else if (cls.isMixinApplication) { |
| 257 if (!cls.isUnnamedMixinApplication) { |
| 258 return SerializedElementKind.NAMED_MIXIN_APPLICATION; |
| 259 } |
| 260 } else { |
| 261 return SerializedElementKind.CLASS; |
| 255 } | 262 } |
| 256 return SerializedElementKind.CLASS; | |
| 257 } | 263 } |
| 258 return null; | 264 return null; |
| 259 } | 265 } |
| 260 | 266 |
| 261 static List<Element> getMembers(ClassElement element) { | 267 static List<Element> getMembers(ClassElement element) { |
| 262 List<Element> members = <Element>[]; | 268 List<Element> members = <Element>[]; |
| 263 element.forEachLocalMember(members.add); | 269 element.forEachLocalMember(members.add); |
| 264 if (element.isPatched) { | 270 if (element.isPatched) { |
| 265 element.implementation.forEachLocalMember((Element member) { | 271 element.implementation.forEachLocalMember((Element member) { |
| 266 if (!member.isPatch) { | 272 if (!member.isPatch) { |
| 267 members.add(member); | 273 members.add(member); |
| 268 } | 274 } |
| 269 }); | 275 }); |
| 270 } | 276 } |
| 271 return members; | 277 return members; |
| 272 } | 278 } |
| 273 | 279 |
| 274 void serialize(ClassElement element, | 280 void serialize(ClassElement element, |
| 275 ObjectEncoder encoder, | 281 ObjectEncoder encoder, |
| 276 SerializedElementKind kind) { | 282 SerializedElementKind kind) { |
| 277 encoder.setElement(Key.LIBRARY, element.library); | 283 encoder.setElement(Key.LIBRARY, element.library); |
| 278 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); | 284 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); |
| 279 encoder.setString(Key.NAME, element.name); | 285 encoder.setString(Key.NAME, element.name); |
| 280 SerializerUtil.serializePosition(element, encoder); | 286 SerializerUtil.serializePosition(element, encoder); |
| 281 encoder.setTypes(Key.TYPE_VARIABLES, element.typeVariables); | 287 encoder.setTypes(Key.TYPE_VARIABLES, element.typeVariables); |
| 282 encoder.setBool(Key.IS_ABSTRACT, element.isAbstract); | 288 encoder.setBool(Key.IS_ABSTRACT, element.isAbstract); |
| 283 if (element.isUnnamedMixinApplication) { | |
| 284 encoder.setBool(Key.IS_UNNAMED_MIXIN_APPLICATION, true); | |
| 285 } | |
| 286 if (element.supertype != null) { | |
| 287 encoder.setType(Key.SUPERTYPE, element.supertype); | |
| 288 } | |
| 289 // TODO(johnniwinther): Make [OrderedTypeSet] easier to (de)serialize. | |
| 290 ObjectEncoder supertypes = encoder.createObject(Key.SUPERTYPES); | |
| 291 supertypes.setTypes(Key.TYPES, | |
| 292 element.allSupertypesAndSelf.types.toList()); | |
| 293 supertypes.setTypes(Key.SUPERTYPES, | |
| 294 element.allSupertypesAndSelf.supertypes.toList()); | |
| 295 supertypes.setInts(Key.OFFSETS, element.allSupertypesAndSelf.levelOffsets); | |
| 296 encoder.setTypes(Key.INTERFACES, element.interfaces.toList()); | |
| 297 SerializerUtil.serializeMembers(getMembers(element), encoder); | 289 SerializerUtil.serializeMembers(getMembers(element), encoder); |
| 298 encoder.setBool(Key.IS_PROXY, element.isProxy); | 290 encoder.setBool(Key.IS_PROXY, element.isProxy); |
| 299 if (kind == SerializedElementKind.ENUM) { | 291 if (kind == SerializedElementKind.ENUM) { |
| 300 EnumClassElement enumClass = element; | 292 EnumClassElement enumClass = element; |
| 301 encoder.setElements(Key.FIELDS, enumClass.enumValues); | 293 encoder.setElements(Key.FIELDS, enumClass.enumValues); |
| 302 } | 294 } |
| 295 if (element.isObject) return; |
| 296 |
| 297 List<InterfaceType> mixins = <InterfaceType>[]; |
| 298 ClassElement superclass = element.superclass; |
| 299 while (superclass.isUnnamedMixinApplication) { |
| 300 MixinApplicationElement mixinElement = superclass; |
| 301 mixins.add(element.thisType.asInstanceOf(mixinElement.mixin)); |
| 302 superclass = mixinElement.superclass; |
| 303 } |
| 304 mixins = mixins.reversed.toList(); |
| 305 InterfaceType supertype = element.thisType.asInstanceOf(superclass); |
| 306 |
| 307 |
| 308 encoder.setType(Key.SUPERTYPE, supertype); |
| 309 encoder.setTypes(Key.MIXINS, mixins); |
| 310 encoder.setTypes(Key.INTERFACES, element.interfaces.toList()); |
| 311 |
| 312 if (element.isMixinApplication) { |
| 313 MixinApplicationElement mixinElement = element; |
| 314 encoder.setType(Key.MIXIN, mixinElement.mixinType); |
| 315 } |
| 303 } | 316 } |
| 304 } | 317 } |
| 305 | 318 |
| 306 class ConstructorSerializer implements ElementSerializer { | 319 class ConstructorSerializer implements ElementSerializer { |
| 307 const ConstructorSerializer(); | 320 const ConstructorSerializer(); |
| 308 | 321 |
| 309 SerializedElementKind getSerializedKind(Element element) { | 322 SerializedElementKind getSerializedKind(Element element) { |
| 310 if (element.isGenerativeConstructor) { | 323 if (element.isGenerativeConstructor) { |
| 311 return SerializedElementKind.GENERATIVE_CONSTRUCTOR; | 324 return SerializedElementKind.GENERATIVE_CONSTRUCTOR; |
| 312 } else if (element.isFactoryConstructor) { | 325 } else if (element.isFactoryConstructor) { |
| 313 return SerializedElementKind.FACTORY_CONSTRUCTOR; | 326 return SerializedElementKind.FACTORY_CONSTRUCTOR; |
| 314 } | 327 } |
| 315 return null; | 328 return null; |
| 316 } | 329 } |
| 317 | 330 |
| 318 void serialize(ConstructorElement element, | 331 void serialize(ConstructorElement element, |
| 319 ObjectEncoder encoder, | 332 ObjectEncoder encoder, |
| 320 SerializedElementKind kind) { | 333 SerializedElementKind kind) { |
| 321 encoder.setElement(Key.CLASS, element.enclosingClass); | 334 encoder.setElement(Key.CLASS, element.enclosingClass); |
| 322 encoder.setType(Key.TYPE, element.type); | 335 encoder.setType(Key.TYPE, element.type); |
| 323 encoder.setString(Key.NAME, element.name); | 336 encoder.setString(Key.NAME, element.name); |
| 324 SerializerUtil.serializePosition(element, encoder); | 337 SerializerUtil.serializePosition(element, encoder); |
| 325 SerializerUtil.serializeParameters(element, encoder); | 338 SerializerUtil.serializeParameters(element, encoder); |
| 326 encoder.setBool(Key.IS_CONST, element.isConst); | 339 encoder.setBool(Key.IS_CONST, element.isConst); |
| 327 // TODO(johnniwinther): Handle external constructors. | |
| 328 encoder.setBool(Key.IS_EXTERNAL, element.isExternal); | 340 encoder.setBool(Key.IS_EXTERNAL, element.isExternal); |
| 329 if (element.isExternal) return; | 341 if (element.isExternal) return; |
| 330 if (element.isConst && !element.isFromEnvironmentConstructor) { | 342 if (element.isConst && !element.isFromEnvironmentConstructor) { |
| 331 ConstantConstructor constantConstructor = element.constantConstructor; | 343 ConstantConstructor constantConstructor = element.constantConstructor; |
| 332 ObjectEncoder constantEncoder = | 344 ObjectEncoder constantEncoder = |
| 333 encoder.createObject(Key.CONSTRUCTOR); | 345 encoder.createObject(Key.CONSTRUCTOR); |
| 334 const ConstantConstructorSerializer().visit( | 346 const ConstantConstructorSerializer().visit( |
| 335 constantConstructor, constantEncoder); | 347 constantConstructor, constantEncoder); |
| 336 } | 348 } |
| 337 } | 349 } |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 586 decoder.getEnum(Key.KIND, SerializedElementKind.values); | 598 decoder.getEnum(Key.KIND, SerializedElementKind.values); |
| 587 switch (elementKind) { | 599 switch (elementKind) { |
| 588 case SerializedElementKind.LIBRARY: | 600 case SerializedElementKind.LIBRARY: |
| 589 return new LibraryElementZ(decoder); | 601 return new LibraryElementZ(decoder); |
| 590 case SerializedElementKind.COMPILATION_UNIT: | 602 case SerializedElementKind.COMPILATION_UNIT: |
| 591 return new CompilationUnitElementZ(decoder); | 603 return new CompilationUnitElementZ(decoder); |
| 592 case SerializedElementKind.CLASS: | 604 case SerializedElementKind.CLASS: |
| 593 return new ClassElementZ(decoder); | 605 return new ClassElementZ(decoder); |
| 594 case SerializedElementKind.ENUM: | 606 case SerializedElementKind.ENUM: |
| 595 return new EnumClassElementZ(decoder); | 607 return new EnumClassElementZ(decoder); |
| 608 case SerializedElementKind.NAMED_MIXIN_APPLICATION: |
| 609 return new NamedMixinApplicationElementZ(decoder); |
| 596 case SerializedElementKind.TOPLEVEL_FIELD: | 610 case SerializedElementKind.TOPLEVEL_FIELD: |
| 597 return new TopLevelFieldElementZ(decoder); | 611 return new TopLevelFieldElementZ(decoder); |
| 598 case SerializedElementKind.STATIC_FIELD: | 612 case SerializedElementKind.STATIC_FIELD: |
| 599 return new StaticFieldElementZ(decoder); | 613 return new StaticFieldElementZ(decoder); |
| 600 case SerializedElementKind.INSTANCE_FIELD: | 614 case SerializedElementKind.INSTANCE_FIELD: |
| 601 return new InstanceFieldElementZ(decoder); | 615 return new InstanceFieldElementZ(decoder); |
| 602 case SerializedElementKind.GENERATIVE_CONSTRUCTOR: | 616 case SerializedElementKind.GENERATIVE_CONSTRUCTOR: |
| 603 return new GenerativeConstructorElementZ(decoder); | 617 return new GenerativeConstructorElementZ(decoder); |
| 604 case SerializedElementKind.FACTORY_CONSTRUCTOR: | 618 case SerializedElementKind.FACTORY_CONSTRUCTOR: |
| 605 return new FactoryConstructorElementZ(decoder); | 619 return new FactoryConstructorElementZ(decoder); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 634 case SerializedElementKind.IMPORT: | 648 case SerializedElementKind.IMPORT: |
| 635 return new ImportElementZ(decoder); | 649 return new ImportElementZ(decoder); |
| 636 case SerializedElementKind.EXPORT: | 650 case SerializedElementKind.EXPORT: |
| 637 return new ExportElementZ(decoder); | 651 return new ExportElementZ(decoder); |
| 638 case SerializedElementKind.PREFIX: | 652 case SerializedElementKind.PREFIX: |
| 639 return new PrefixElementZ(decoder); | 653 return new PrefixElementZ(decoder); |
| 640 } | 654 } |
| 641 throw new UnsupportedError("Unexpected element kind '${elementKind}."); | 655 throw new UnsupportedError("Unexpected element kind '${elementKind}."); |
| 642 } | 656 } |
| 643 } | 657 } |
| OLD | NEW |