| 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'; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 INSTANCE_FIELD, | 27 INSTANCE_FIELD, |
| 28 TOPLEVEL_FUNCTION, | 28 TOPLEVEL_FUNCTION, |
| 29 TOPLEVEL_GETTER, | 29 TOPLEVEL_GETTER, |
| 30 TOPLEVEL_SETTER, | 30 TOPLEVEL_SETTER, |
| 31 STATIC_FUNCTION, | 31 STATIC_FUNCTION, |
| 32 STATIC_GETTER, | 32 STATIC_GETTER, |
| 33 STATIC_SETTER, | 33 STATIC_SETTER, |
| 34 INSTANCE_FUNCTION, | 34 INSTANCE_FUNCTION, |
| 35 INSTANCE_GETTER, | 35 INSTANCE_GETTER, |
| 36 INSTANCE_SETTER, | 36 INSTANCE_SETTER, |
| 37 LOCAL_FUNCTION, |
| 37 TYPEDEF, | 38 TYPEDEF, |
| 38 TYPEVARIABLE, | 39 TYPEVARIABLE, |
| 39 PARAMETER, | 40 PARAMETER, |
| 40 INITIALIZING_FORMAL, | 41 INITIALIZING_FORMAL, |
| 41 IMPORT, | 42 IMPORT, |
| 42 EXPORT, | 43 EXPORT, |
| 43 PREFIX, | 44 PREFIX, |
| 44 } | 45 } |
| 45 | 46 |
| 46 /// Set of serializers used to serialize different kinds of elements by | 47 /// Set of serializers used to serialize different kinds of elements by |
| (...skipping 26 matching lines...) Expand all Loading... |
| 73 | 74 |
| 74 /// Serializes [element] into the [encoder] using the [kind] computed | 75 /// Serializes [element] into the [encoder] using the [kind] computed |
| 75 /// by [getSerializedKind]. | 76 /// by [getSerializedKind]. |
| 76 void serialize(Element element, | 77 void serialize(Element element, |
| 77 ObjectEncoder encoder, | 78 ObjectEncoder encoder, |
| 78 SerializedElementKind kind); | 79 SerializedElementKind kind); |
| 79 } | 80 } |
| 80 | 81 |
| 81 class SerializerUtil { | 82 class SerializerUtil { |
| 82 /// Serialize the declared members of [element] into [encoder]. | 83 /// Serialize the declared members of [element] into [encoder]. |
| 83 static void serializeMembers(ScopeContainerElement element, | 84 static void serializeMembers(Iterable<Element> members, |
| 84 ObjectEncoder encoder) { | 85 ObjectEncoder encoder) { |
| 85 MapEncoder mapEncoder = encoder.createMap(Key.MEMBERS); | 86 MapEncoder mapEncoder = encoder.createMap(Key.MEMBERS); |
| 86 element.forEachLocalMember((Element member) { | 87 for (Element member in members) { |
| 87 String name = member.name; | 88 String name = member.name; |
| 88 if (member.isSetter) { | 89 if (member.isSetter) { |
| 89 name = '$name,='; | 90 name = '$name,='; |
| 90 } | 91 } |
| 91 mapEncoder.setElement(name, member); | 92 mapEncoder.setElement(name, member); |
| 92 }); | 93 } |
| 93 } | 94 } |
| 94 | 95 |
| 95 /// Serialize the source position of [element] into [encoder]. | 96 /// Serialize the source position of [element] into [encoder]. |
| 96 static void serializePosition(Element element, ObjectEncoder encoder) { | 97 static void serializePosition(Element element, ObjectEncoder encoder) { |
| 97 if (element.sourcePosition != null) { | 98 if (element.sourcePosition != null) { |
| 98 SourceSpan position = element.sourcePosition; | 99 SourceSpan position = element.sourcePosition; |
| 99 encoder.setInt(Key.OFFSET, position.begin); | 100 encoder.setInt(Key.OFFSET, position.begin); |
| 100 if (position.uri != element.compilationUnit.script.resourceUri) { | 101 if (position.uri != element.compilationUnit.script.resourceUri) { |
| 101 // TODO(johnniwinther): What is the base URI in the case? | 102 // TODO(johnniwinther): What is the base URI in the case? |
| 102 encoder.setUri(Key.URI, element.library.canonicalUri, position.uri); | 103 encoder.setUri(Key.URI, element.library.canonicalUri, position.uri); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 class LibrarySerializer implements ElementSerializer { | 145 class LibrarySerializer implements ElementSerializer { |
| 145 const LibrarySerializer(); | 146 const LibrarySerializer(); |
| 146 | 147 |
| 147 SerializedElementKind getSerializedKind(Element element) { | 148 SerializedElementKind getSerializedKind(Element element) { |
| 148 if (element.isLibrary) { | 149 if (element.isLibrary) { |
| 149 return SerializedElementKind.LIBRARY; | 150 return SerializedElementKind.LIBRARY; |
| 150 } | 151 } |
| 151 return null; | 152 return null; |
| 152 } | 153 } |
| 153 | 154 |
| 155 static List<Element> getMembers(LibraryElement element) { |
| 156 List<Element> members = <Element>[]; |
| 157 element.implementation.forEachLocalMember((Element member) { |
| 158 if (!member.isPatch) { |
| 159 members.add(member); |
| 160 } |
| 161 }); |
| 162 return members; |
| 163 } |
| 164 |
| 154 static List<CompilationUnitElement> getCompilationUnits( | 165 static List<CompilationUnitElement> getCompilationUnits( |
| 155 LibraryElement element) { | 166 LibraryElement element) { |
| 156 List<CompilationUnitElement> compilationUnits = <CompilationUnitElement>[]; | 167 List<CompilationUnitElement> compilationUnits = <CompilationUnitElement>[]; |
| 157 compilationUnits.addAll(element.compilationUnits.toList()); | 168 compilationUnits.addAll(element.compilationUnits.toList()); |
| 158 if (element.isPatched) { | 169 if (element.isPatched) { |
| 159 compilationUnits.addAll(element.implementation.compilationUnits.toList()); | 170 compilationUnits.addAll(element.implementation.compilationUnits.toList()); |
| 160 } | 171 } |
| 161 return compilationUnits; | 172 return compilationUnits; |
| 162 } | 173 } |
| 163 | 174 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 187 element.forEachExport(SerializerUtil.flattenElements(exportedElements)); | 198 element.forEachExport(SerializerUtil.flattenElements(exportedElements)); |
| 188 return exportedElements.toList(); | 199 return exportedElements.toList(); |
| 189 } | 200 } |
| 190 | 201 |
| 191 void serialize(LibraryElement element, | 202 void serialize(LibraryElement element, |
| 192 ObjectEncoder encoder, | 203 ObjectEncoder encoder, |
| 193 SerializedElementKind kind) { | 204 SerializedElementKind kind) { |
| 194 encoder.setUri( | 205 encoder.setUri( |
| 195 Key.CANONICAL_URI, element.canonicalUri, element.canonicalUri); | 206 Key.CANONICAL_URI, element.canonicalUri, element.canonicalUri); |
| 196 encoder.setString(Key.LIBRARY_NAME, element.libraryName); | 207 encoder.setString(Key.LIBRARY_NAME, element.libraryName); |
| 197 SerializerUtil.serializeMembers(element, encoder); | 208 SerializerUtil.serializeMembers(getMembers(element), encoder); |
| 198 encoder.setElement(Key.COMPILATION_UNIT, element.entryCompilationUnit); | 209 encoder.setElement(Key.COMPILATION_UNIT, element.entryCompilationUnit); |
| 199 encoder.setElements(Key.COMPILATION_UNITS, getCompilationUnits(element)); | 210 encoder.setElements(Key.COMPILATION_UNITS, getCompilationUnits(element)); |
| 200 encoder.setElements(Key.IMPORTS, getImports(element)); | 211 encoder.setElements(Key.IMPORTS, getImports(element)); |
| 201 encoder.setElements(Key.EXPORTS, element.exports); | 212 encoder.setElements(Key.EXPORTS, element.exports); |
| 202 | 213 |
| 203 encoder.setElements(Key.IMPORT_SCOPE, getImportedElements(element)); | 214 encoder.setElements(Key.IMPORT_SCOPE, getImportedElements(element)); |
| 204 | 215 |
| 205 encoder.setElements(Key.EXPORT_SCOPE, getExportedElements(element)); | 216 encoder.setElements(Key.EXPORT_SCOPE, getExportedElements(element)); |
| 206 | 217 |
| 207 } | 218 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 if (element.isClass) { | 251 if (element.isClass) { |
| 241 ClassElement cls = element; | 252 ClassElement cls = element; |
| 242 if (cls.isEnumClass) { | 253 if (cls.isEnumClass) { |
| 243 return SerializedElementKind.ENUM; | 254 return SerializedElementKind.ENUM; |
| 244 } | 255 } |
| 245 return SerializedElementKind.CLASS; | 256 return SerializedElementKind.CLASS; |
| 246 } | 257 } |
| 247 return null; | 258 return null; |
| 248 } | 259 } |
| 249 | 260 |
| 261 static List<Element> getMembers(ClassElement element) { |
| 262 List<Element> members = <Element>[]; |
| 263 element.forEachLocalMember(members.add); |
| 264 if (element.isPatched) { |
| 265 element.implementation.forEachLocalMember((Element member) { |
| 266 if (!member.isPatch) { |
| 267 members.add(member); |
| 268 } |
| 269 }); |
| 270 } |
| 271 return members; |
| 272 } |
| 273 |
| 250 void serialize(ClassElement element, | 274 void serialize(ClassElement element, |
| 251 ObjectEncoder encoder, | 275 ObjectEncoder encoder, |
| 252 SerializedElementKind kind) { | 276 SerializedElementKind kind) { |
| 253 encoder.setElement(Key.LIBRARY, element.library); | 277 encoder.setElement(Key.LIBRARY, element.library); |
| 254 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); | 278 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); |
| 255 encoder.setString(Key.NAME, element.name); | 279 encoder.setString(Key.NAME, element.name); |
| 256 SerializerUtil.serializePosition(element, encoder); | 280 SerializerUtil.serializePosition(element, encoder); |
| 257 encoder.setTypes(Key.TYPE_VARIABLES, element.typeVariables); | 281 encoder.setTypes(Key.TYPE_VARIABLES, element.typeVariables); |
| 258 encoder.setBool(Key.IS_ABSTRACT, element.isAbstract); | 282 encoder.setBool(Key.IS_ABSTRACT, element.isAbstract); |
| 259 if (element.isUnnamedMixinApplication) { | 283 if (element.isUnnamedMixinApplication) { |
| 260 encoder.setBool(Key.IS_UNNAMED_MIXIN_APPLICATION, true); | 284 encoder.setBool(Key.IS_UNNAMED_MIXIN_APPLICATION, true); |
| 261 } | 285 } |
| 262 if (element.supertype != null) { | 286 if (element.supertype != null) { |
| 263 encoder.setType(Key.SUPERTYPE, element.supertype); | 287 encoder.setType(Key.SUPERTYPE, element.supertype); |
| 264 } | 288 } |
| 265 // TODO(johnniwinther): Make [OrderedTypeSet] easier to (de)serialize. | 289 // TODO(johnniwinther): Make [OrderedTypeSet] easier to (de)serialize. |
| 266 ObjectEncoder supertypes = encoder.createObject(Key.SUPERTYPES); | 290 ObjectEncoder supertypes = encoder.createObject(Key.SUPERTYPES); |
| 267 supertypes.setTypes(Key.TYPES, | 291 supertypes.setTypes(Key.TYPES, |
| 268 element.allSupertypesAndSelf.types.toList()); | 292 element.allSupertypesAndSelf.types.toList()); |
| 269 supertypes.setTypes(Key.SUPERTYPES, | 293 supertypes.setTypes(Key.SUPERTYPES, |
| 270 element.allSupertypesAndSelf.supertypes.toList()); | 294 element.allSupertypesAndSelf.supertypes.toList()); |
| 271 supertypes.setInts(Key.OFFSETS, element.allSupertypesAndSelf.levelOffsets); | 295 supertypes.setInts(Key.OFFSETS, element.allSupertypesAndSelf.levelOffsets); |
| 272 encoder.setTypes(Key.INTERFACES, element.interfaces.toList()); | 296 encoder.setTypes(Key.INTERFACES, element.interfaces.toList()); |
| 273 SerializerUtil.serializeMembers(element, encoder); | 297 SerializerUtil.serializeMembers(getMembers(element), encoder); |
| 298 encoder.setBool(Key.IS_PROXY, element.isProxy); |
| 274 if (kind == SerializedElementKind.ENUM) { | 299 if (kind == SerializedElementKind.ENUM) { |
| 275 EnumClassElement enumClass = element; | 300 EnumClassElement enumClass = element; |
| 276 encoder.setElements(Key.FIELDS, enumClass.enumValues); | 301 encoder.setElements(Key.FIELDS, enumClass.enumValues); |
| 277 } | 302 } |
| 278 } | 303 } |
| 279 } | 304 } |
| 280 | 305 |
| 281 class ConstructorSerializer implements ElementSerializer { | 306 class ConstructorSerializer implements ElementSerializer { |
| 282 const ConstructorSerializer(); | 307 const ConstructorSerializer(); |
| 283 | 308 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 class FunctionSerializer implements ElementSerializer { | 373 class FunctionSerializer implements ElementSerializer { |
| 349 const FunctionSerializer(); | 374 const FunctionSerializer(); |
| 350 | 375 |
| 351 SerializedElementKind getSerializedKind(Element element) { | 376 SerializedElementKind getSerializedKind(Element element) { |
| 352 if (element.isFunction) { | 377 if (element.isFunction) { |
| 353 if (element.isTopLevel) return SerializedElementKind.TOPLEVEL_FUNCTION; | 378 if (element.isTopLevel) return SerializedElementKind.TOPLEVEL_FUNCTION; |
| 354 if (element.isStatic) return SerializedElementKind.STATIC_FUNCTION; | 379 if (element.isStatic) return SerializedElementKind.STATIC_FUNCTION; |
| 355 if (element.isInstanceMember) { | 380 if (element.isInstanceMember) { |
| 356 return SerializedElementKind.INSTANCE_FUNCTION; | 381 return SerializedElementKind.INSTANCE_FUNCTION; |
| 357 } | 382 } |
| 383 if (element.isLocal) { |
| 384 return SerializedElementKind.LOCAL_FUNCTION; |
| 385 } |
| 358 } | 386 } |
| 359 if (element.isGetter) { | 387 if (element.isGetter) { |
| 360 if (element.isTopLevel) return SerializedElementKind.TOPLEVEL_GETTER; | 388 if (element.isTopLevel) return SerializedElementKind.TOPLEVEL_GETTER; |
| 361 if (element.isStatic) return SerializedElementKind.STATIC_GETTER; | 389 if (element.isStatic) return SerializedElementKind.STATIC_GETTER; |
| 362 if (element.isInstanceMember) { | 390 if (element.isInstanceMember) { |
| 363 return SerializedElementKind.INSTANCE_GETTER; | 391 return SerializedElementKind.INSTANCE_GETTER; |
| 364 } | 392 } |
| 365 } | 393 } |
| 366 if (element.isSetter) { | 394 if (element.isSetter) { |
| 367 if (element.isTopLevel) return SerializedElementKind.TOPLEVEL_SETTER; | 395 if (element.isTopLevel) return SerializedElementKind.TOPLEVEL_SETTER; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 383 if (element.isFunction) { | 411 if (element.isFunction) { |
| 384 encoder.setBool(Key.IS_OPERATOR, element.isOperator); | 412 encoder.setBool(Key.IS_OPERATOR, element.isOperator); |
| 385 } | 413 } |
| 386 if (element.enclosingClass != null) { | 414 if (element.enclosingClass != null) { |
| 387 encoder.setElement(Key.CLASS, element.enclosingClass); | 415 encoder.setElement(Key.CLASS, element.enclosingClass); |
| 388 } else { | 416 } else { |
| 389 encoder.setElement(Key.LIBRARY, element.library); | 417 encoder.setElement(Key.LIBRARY, element.library); |
| 390 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); | 418 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); |
| 391 } | 419 } |
| 392 encoder.setBool(Key.IS_EXTERNAL, element.isExternal); | 420 encoder.setBool(Key.IS_EXTERNAL, element.isExternal); |
| 421 if (element.isLocal) { |
| 422 LocalFunctionElement localFunction = element; |
| 423 encoder.setElement( |
| 424 Key.EXECUTABLE_CONTEXT, localFunction.executableContext); |
| 425 } |
| 393 } | 426 } |
| 394 } | 427 } |
| 395 | 428 |
| 396 class TypedefSerializer implements ElementSerializer { | 429 class TypedefSerializer implements ElementSerializer { |
| 397 const TypedefSerializer(); | 430 const TypedefSerializer(); |
| 398 | 431 |
| 399 SerializedElementKind getSerializedKind(Element element) { | 432 SerializedElementKind getSerializedKind(Element element) { |
| 400 if (element.isTypedef) { | 433 if (element.isTypedef) { |
| 401 return SerializedElementKind.TYPEDEF; | 434 return SerializedElementKind.TYPEDEF; |
| 402 } | 435 } |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 569 case SerializedElementKind.GENERATIVE_CONSTRUCTOR: | 602 case SerializedElementKind.GENERATIVE_CONSTRUCTOR: |
| 570 return new GenerativeConstructorElementZ(decoder); | 603 return new GenerativeConstructorElementZ(decoder); |
| 571 case SerializedElementKind.FACTORY_CONSTRUCTOR: | 604 case SerializedElementKind.FACTORY_CONSTRUCTOR: |
| 572 return new FactoryConstructorElementZ(decoder); | 605 return new FactoryConstructorElementZ(decoder); |
| 573 case SerializedElementKind.TOPLEVEL_FUNCTION: | 606 case SerializedElementKind.TOPLEVEL_FUNCTION: |
| 574 return new TopLevelFunctionElementZ(decoder); | 607 return new TopLevelFunctionElementZ(decoder); |
| 575 case SerializedElementKind.STATIC_FUNCTION: | 608 case SerializedElementKind.STATIC_FUNCTION: |
| 576 return new StaticFunctionElementZ(decoder); | 609 return new StaticFunctionElementZ(decoder); |
| 577 case SerializedElementKind.INSTANCE_FUNCTION: | 610 case SerializedElementKind.INSTANCE_FUNCTION: |
| 578 return new InstanceFunctionElementZ(decoder); | 611 return new InstanceFunctionElementZ(decoder); |
| 612 case SerializedElementKind.LOCAL_FUNCTION: |
| 613 return new LocalFunctionElementZ(decoder); |
| 579 case SerializedElementKind.TOPLEVEL_GETTER: | 614 case SerializedElementKind.TOPLEVEL_GETTER: |
| 580 return new TopLevelGetterElementZ(decoder); | 615 return new TopLevelGetterElementZ(decoder); |
| 581 case SerializedElementKind.STATIC_GETTER: | 616 case SerializedElementKind.STATIC_GETTER: |
| 582 return new StaticGetterElementZ(decoder); | 617 return new StaticGetterElementZ(decoder); |
| 583 case SerializedElementKind.INSTANCE_GETTER: | 618 case SerializedElementKind.INSTANCE_GETTER: |
| 584 return new InstanceGetterElementZ(decoder); | 619 return new InstanceGetterElementZ(decoder); |
| 585 case SerializedElementKind.TOPLEVEL_SETTER: | 620 case SerializedElementKind.TOPLEVEL_SETTER: |
| 586 return new TopLevelSetterElementZ(decoder); | 621 return new TopLevelSetterElementZ(decoder); |
| 587 case SerializedElementKind.STATIC_SETTER: | 622 case SerializedElementKind.STATIC_SETTER: |
| 588 return new StaticSetterElementZ(decoder); | 623 return new StaticSetterElementZ(decoder); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 599 case SerializedElementKind.IMPORT: | 634 case SerializedElementKind.IMPORT: |
| 600 return new ImportElementZ(decoder); | 635 return new ImportElementZ(decoder); |
| 601 case SerializedElementKind.EXPORT: | 636 case SerializedElementKind.EXPORT: |
| 602 return new ExportElementZ(decoder); | 637 return new ExportElementZ(decoder); |
| 603 case SerializedElementKind.PREFIX: | 638 case SerializedElementKind.PREFIX: |
| 604 return new PrefixElementZ(decoder); | 639 return new PrefixElementZ(decoder); |
| 605 } | 640 } |
| 606 throw new UnsupportedError("Unexpected element kind '${elementKind}."); | 641 throw new UnsupportedError("Unexpected element kind '${elementKind}."); |
| 607 } | 642 } |
| 608 } | 643 } |
| OLD | NEW |