| 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 } | 50 } |
| 51 | 51 |
| 52 /// Set of serializers used to serialize different kinds of elements by | 52 /// Set of serializers used to serialize different kinds of elements by |
| 53 /// encoding into them into [ObjectEncoder]s. | 53 /// encoding into them into [ObjectEncoder]s. |
| 54 /// | 54 /// |
| 55 /// This class is called from the [Serializer] when an [Element] needs | 55 /// This class is called from the [Serializer] when an [Element] needs |
| 56 /// serialization. The [ObjectEncoder] ensures that any [Element], [DartType], | 56 /// serialization. The [ObjectEncoder] ensures that any [Element], [DartType], |
| 57 /// and [ConstantExpression] that the serialized [Element] depends upon are also | 57 /// and [ConstantExpression] that the serialized [Element] depends upon are also |
| 58 /// serialized. | 58 /// serialized. |
| 59 const List<ElementSerializer> ELEMENT_SERIALIZERS = const [ | 59 const List<ElementSerializer> ELEMENT_SERIALIZERS = const [ |
| 60 const LibrarySerializer(), | 60 const LibrarySerializer(), |
| 61 const CompilationUnitSerializer(), | 61 const CompilationUnitSerializer(), |
| 62 const ClassSerializer(), | 62 const ClassSerializer(), |
| 63 const ConstructorSerializer(), | 63 const ConstructorSerializer(), |
| 64 const FieldSerializer(), | 64 const FieldSerializer(), |
| 65 const FunctionSerializer(), | 65 const FunctionSerializer(), |
| 66 const TypedefSerializer(), | 66 const TypedefSerializer(), |
| 67 const TypeVariableSerializer(), | 67 const TypeVariableSerializer(), |
| 68 const ParameterSerializer(), | 68 const ParameterSerializer(), |
| 69 const ImportSerializer(), | 69 const ImportSerializer(), |
| 70 const ExportSerializer(), | 70 const ExportSerializer(), |
| 71 const PrefixSerializer(), | 71 const PrefixSerializer(), |
| 72 ]; | 72 ]; |
| 73 | 73 |
| 74 /// Interface for a function that can serialize a set of element kinds. | 74 /// Interface for a function that can serialize a set of element kinds. |
| 75 abstract class ElementSerializer { | 75 abstract class ElementSerializer { |
| 76 /// Returns the [SerializedElementKind] for [element] if this serializer | 76 /// Returns the [SerializedElementKind] for [element] if this serializer |
| 77 /// supports serialization of [element] or `null` otherwise. | 77 /// supports serialization of [element] or `null` otherwise. |
| 78 SerializedElementKind getSerializedKind(Element element); | 78 SerializedElementKind getSerializedKind(Element element); |
| 79 | 79 |
| 80 /// Serializes [element] into the [encoder] using the [kind] computed | 80 /// Serializes [element] into the [encoder] using the [kind] computed |
| 81 /// by [getSerializedKind]. | 81 /// by [getSerializedKind]. |
| 82 void serialize(Element element, | 82 void serialize( |
| 83 ObjectEncoder encoder, | 83 Element element, ObjectEncoder encoder, SerializedElementKind kind); |
| 84 SerializedElementKind kind); | |
| 85 } | 84 } |
| 86 | 85 |
| 87 class SerializerUtil { | 86 class SerializerUtil { |
| 88 /// Serialize the declared members of [element] into [encoder]. | 87 /// Serialize the declared members of [element] into [encoder]. |
| 89 static void serializeMembers(Iterable<Element> members, | 88 static void serializeMembers( |
| 90 ObjectEncoder encoder) { | 89 Iterable<Element> members, ObjectEncoder encoder) { |
| 91 MapEncoder mapEncoder = encoder.createMap(Key.MEMBERS); | 90 MapEncoder mapEncoder = encoder.createMap(Key.MEMBERS); |
| 92 for (Element member in members) { | 91 for (Element member in members) { |
| 93 String name = member.name; | 92 String name = member.name; |
| 94 if (member.isSetter) { | 93 if (member.isSetter) { |
| 95 name = '$name,='; | 94 name = '$name,='; |
| 96 } | 95 } |
| 97 mapEncoder.setElement(name, member); | 96 mapEncoder.setElement(name, member); |
| 98 } | 97 } |
| 99 } | 98 } |
| 100 | 99 |
| 101 /// Serialize the source position of [element] into [encoder]. | 100 /// Serialize the source position of [element] into [encoder]. |
| 102 static void serializePosition(Element element, ObjectEncoder encoder) { | 101 static void serializePosition(Element element, ObjectEncoder encoder) { |
| 103 if (element.sourcePosition != null) { | 102 if (element.sourcePosition != null) { |
| 104 SourceSpan position = element.sourcePosition; | 103 SourceSpan position = element.sourcePosition; |
| 105 encoder.setInt(Key.OFFSET, position.begin); | 104 encoder.setInt(Key.OFFSET, position.begin); |
| 106 if (position.uri != element.compilationUnit.script.resourceUri) { | 105 if (position.uri != element.compilationUnit.script.resourceUri) { |
| 107 // TODO(johnniwinther): What is the base URI in the case? | 106 // TODO(johnniwinther): What is the base URI in the case? |
| 108 encoder.setUri(Key.URI, element.library.canonicalUri, position.uri); | 107 encoder.setUri(Key.URI, element.library.canonicalUri, position.uri); |
| 109 } | 108 } |
| 110 int length = position.end - position.begin; | 109 int length = position.end - position.begin; |
| 111 if (element.name.length != length) { | 110 if (element.name.length != length) { |
| 112 encoder.setInt(Key.LENGTH, length); | 111 encoder.setInt(Key.LENGTH, length); |
| 113 } | 112 } |
| 114 } | 113 } |
| 115 } | 114 } |
| 116 | 115 |
| 117 /// Serialize the parameters of [element] into [encoder]. | 116 /// Serialize the parameters of [element] into [encoder]. |
| 118 static void serializeParameters(FunctionElement element, | 117 static void serializeParameters( |
| 119 ObjectEncoder encoder) { | 118 FunctionElement element, ObjectEncoder encoder) { |
| 120 FunctionType type = element.type; | 119 FunctionType type = element.type; |
| 121 encoder.setType(Key.RETURN_TYPE, type.returnType); | 120 encoder.setType(Key.RETURN_TYPE, type.returnType); |
| 122 encoder.setElements(Key.PARAMETERS, element.parameters); | 121 encoder.setElements(Key.PARAMETERS, element.parameters); |
| 123 } | 122 } |
| 124 | 123 |
| 125 /// Returns a function that adds the underlying declared elements for a | 124 /// Returns a function that adds the underlying declared elements for a |
| 126 /// particular element into [set]. | 125 /// particular element into [set]. |
| 127 /// | 126 /// |
| 128 /// For instance, for an [AbstractFieldElement] the getter and setter elements | 127 /// For instance, for an [AbstractFieldElement] the getter and setter elements |
| 129 /// are added, if available. | 128 /// are added, if available. |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 | 178 |
| 180 static List<ImportElement> getImports(LibraryElement element) { | 179 static List<ImportElement> getImports(LibraryElement element) { |
| 181 List<ImportElement> imports = <ImportElement>[]; | 180 List<ImportElement> imports = <ImportElement>[]; |
| 182 imports.addAll(element.imports); | 181 imports.addAll(element.imports); |
| 183 if (element.isPatched) { | 182 if (element.isPatched) { |
| 184 imports.addAll(element.implementation.imports); | 183 imports.addAll(element.implementation.imports); |
| 185 } | 184 } |
| 186 return imports; | 185 return imports; |
| 187 } | 186 } |
| 188 | 187 |
| 189 static List<Element> getImportedElements( | 188 static List<Element> getImportedElements(LibraryElement element) { |
| 190 LibraryElement element) { | |
| 191 Set<Element> importedElements = new Set<Element>(); | 189 Set<Element> importedElements = new Set<Element>(); |
| 192 element.forEachImport(SerializerUtil.flattenElements(importedElements)); | 190 element.forEachImport(SerializerUtil.flattenElements(importedElements)); |
| 193 if (element.isPatched) { | 191 if (element.isPatched) { |
| 194 element.implementation.forEachImport( | 192 element.implementation |
| 195 SerializerUtil.flattenElements(importedElements)); | 193 .forEachImport(SerializerUtil.flattenElements(importedElements)); |
| 196 } | 194 } |
| 197 return importedElements.toList(); | 195 return importedElements.toList(); |
| 198 } | 196 } |
| 199 | 197 |
| 200 static List<Element> getExportedElements( | 198 static List<Element> getExportedElements(LibraryElement element) { |
| 201 LibraryElement element) { | |
| 202 Set<Element> exportedElements = new Set<Element>(); | 199 Set<Element> exportedElements = new Set<Element>(); |
| 203 element.forEachExport(SerializerUtil.flattenElements(exportedElements)); | 200 element.forEachExport(SerializerUtil.flattenElements(exportedElements)); |
| 204 return exportedElements.toList(); | 201 return exportedElements.toList(); |
| 205 } | 202 } |
| 206 | 203 |
| 207 void serialize(LibraryElement element, | 204 void serialize(LibraryElement element, ObjectEncoder encoder, |
| 208 ObjectEncoder encoder, | 205 SerializedElementKind kind) { |
| 209 SerializedElementKind kind) { | |
| 210 encoder.setUri( | 206 encoder.setUri( |
| 211 Key.CANONICAL_URI, element.canonicalUri, element.canonicalUri); | 207 Key.CANONICAL_URI, element.canonicalUri, element.canonicalUri); |
| 212 encoder.setString(Key.LIBRARY_NAME, element.libraryName); | 208 encoder.setString(Key.LIBRARY_NAME, element.libraryName); |
| 213 SerializerUtil.serializeMembers(getMembers(element), encoder); | 209 SerializerUtil.serializeMembers(getMembers(element), encoder); |
| 214 encoder.setElement(Key.COMPILATION_UNIT, element.entryCompilationUnit); | 210 encoder.setElement(Key.COMPILATION_UNIT, element.entryCompilationUnit); |
| 215 encoder.setElements(Key.COMPILATION_UNITS, getCompilationUnits(element)); | 211 encoder.setElements(Key.COMPILATION_UNITS, getCompilationUnits(element)); |
| 216 encoder.setElements(Key.IMPORTS, getImports(element)); | 212 encoder.setElements(Key.IMPORTS, getImports(element)); |
| 217 encoder.setElements(Key.EXPORTS, element.exports); | 213 encoder.setElements(Key.EXPORTS, element.exports); |
| 218 | 214 |
| 219 encoder.setElements(Key.IMPORT_SCOPE, getImportedElements(element)); | 215 encoder.setElements(Key.IMPORT_SCOPE, getImportedElements(element)); |
| 220 | 216 |
| 221 encoder.setElements(Key.EXPORT_SCOPE, getExportedElements(element)); | 217 encoder.setElements(Key.EXPORT_SCOPE, getExportedElements(element)); |
| 222 | |
| 223 } | 218 } |
| 224 } | 219 } |
| 225 | 220 |
| 226 class CompilationUnitSerializer implements ElementSerializer { | 221 class CompilationUnitSerializer implements ElementSerializer { |
| 227 const CompilationUnitSerializer(); | 222 const CompilationUnitSerializer(); |
| 228 | 223 |
| 229 SerializedElementKind getSerializedKind(Element element) { | 224 SerializedElementKind getSerializedKind(Element element) { |
| 230 if (element.isCompilationUnit) { | 225 if (element.isCompilationUnit) { |
| 231 return SerializedElementKind.COMPILATION_UNIT; | 226 return SerializedElementKind.COMPILATION_UNIT; |
| 232 } | 227 } |
| 233 return null; | 228 return null; |
| 234 } | 229 } |
| 235 | 230 |
| 236 void serialize(CompilationUnitElement element, | 231 void serialize(CompilationUnitElement element, ObjectEncoder encoder, |
| 237 ObjectEncoder encoder, | 232 SerializedElementKind kind) { |
| 238 SerializedElementKind kind) { | |
| 239 encoder.setElement(Key.LIBRARY, element.library); | 233 encoder.setElement(Key.LIBRARY, element.library); |
| 240 encoder.setUri( | 234 encoder.setUri( |
| 241 Key.URI, element.library.canonicalUri, element.script.resourceUri); | 235 Key.URI, element.library.canonicalUri, element.script.resourceUri); |
| 242 List<Element> elements = <Element>[]; | 236 List<Element> elements = <Element>[]; |
| 243 element.forEachLocalMember((e) { | 237 element.forEachLocalMember((e) { |
| 244 if (!element.isPatch) { | 238 if (!element.isPatch) { |
| 245 elements.add(e); | 239 elements.add(e); |
| 246 } | 240 } |
| 247 }); | 241 }); |
| 248 encoder.setElements(Key.ELEMENTS, elements); | 242 encoder.setElements(Key.ELEMENTS, elements); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 274 if (element.isPatched) { | 268 if (element.isPatched) { |
| 275 element.implementation.forEachLocalMember((Element member) { | 269 element.implementation.forEachLocalMember((Element member) { |
| 276 if (!member.isPatch) { | 270 if (!member.isPatch) { |
| 277 members.add(member); | 271 members.add(member); |
| 278 } | 272 } |
| 279 }); | 273 }); |
| 280 } | 274 } |
| 281 return members; | 275 return members; |
| 282 } | 276 } |
| 283 | 277 |
| 284 void serialize(ClassElement element, | 278 void serialize( |
| 285 ObjectEncoder encoder, | 279 ClassElement element, ObjectEncoder encoder, SerializedElementKind kind) { |
| 286 SerializedElementKind kind) { | |
| 287 encoder.setElement(Key.LIBRARY, element.library); | 280 encoder.setElement(Key.LIBRARY, element.library); |
| 288 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); | 281 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); |
| 289 encoder.setString(Key.NAME, element.name); | 282 encoder.setString(Key.NAME, element.name); |
| 290 SerializerUtil.serializePosition(element, encoder); | 283 SerializerUtil.serializePosition(element, encoder); |
| 291 encoder.setTypes(Key.TYPE_VARIABLES, element.typeVariables); | 284 encoder.setTypes(Key.TYPE_VARIABLES, element.typeVariables); |
| 292 encoder.setBool(Key.IS_ABSTRACT, element.isAbstract); | 285 encoder.setBool(Key.IS_ABSTRACT, element.isAbstract); |
| 293 SerializerUtil.serializeMembers(getMembers(element), encoder); | 286 SerializerUtil.serializeMembers(getMembers(element), encoder); |
| 294 encoder.setBool(Key.IS_PROXY, element.isProxy); | 287 encoder.setBool(Key.IS_PROXY, element.isProxy); |
| 295 if (kind == SerializedElementKind.ENUM) { | 288 if (kind == SerializedElementKind.ENUM) { |
| 296 EnumClassElement enumClass = element; | 289 EnumClassElement enumClass = element; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 | 321 |
| 329 SerializedElementKind getSerializedKind(Element element) { | 322 SerializedElementKind getSerializedKind(Element element) { |
| 330 if (element.isGenerativeConstructor) { | 323 if (element.isGenerativeConstructor) { |
| 331 return SerializedElementKind.GENERATIVE_CONSTRUCTOR; | 324 return SerializedElementKind.GENERATIVE_CONSTRUCTOR; |
| 332 } else if (element.isFactoryConstructor) { | 325 } else if (element.isFactoryConstructor) { |
| 333 return SerializedElementKind.FACTORY_CONSTRUCTOR; | 326 return SerializedElementKind.FACTORY_CONSTRUCTOR; |
| 334 } | 327 } |
| 335 return null; | 328 return null; |
| 336 } | 329 } |
| 337 | 330 |
| 338 void serialize(ConstructorElement element, | 331 void serialize(ConstructorElement element, ObjectEncoder encoder, |
| 339 ObjectEncoder encoder, | 332 SerializedElementKind kind) { |
| 340 SerializedElementKind kind) { | |
| 341 encoder.setElement(Key.CLASS, element.enclosingClass); | 333 encoder.setElement(Key.CLASS, element.enclosingClass); |
| 342 encoder.setType(Key.TYPE, element.type); | 334 encoder.setType(Key.TYPE, element.type); |
| 343 encoder.setString(Key.NAME, element.name); | 335 encoder.setString(Key.NAME, element.name); |
| 344 SerializerUtil.serializePosition(element, encoder); | 336 SerializerUtil.serializePosition(element, encoder); |
| 345 SerializerUtil.serializeParameters(element, encoder); | 337 SerializerUtil.serializeParameters(element, encoder); |
| 346 encoder.setBool(Key.IS_CONST, element.isConst); | 338 encoder.setBool(Key.IS_CONST, element.isConst); |
| 347 encoder.setBool(Key.IS_EXTERNAL, element.isExternal); | 339 encoder.setBool(Key.IS_EXTERNAL, element.isExternal); |
| 348 if (element.isExternal) return; | 340 if (element.isExternal) return; |
| 349 if (element.isConst && !element.isFromEnvironmentConstructor) { | 341 if (element.isConst && !element.isFromEnvironmentConstructor) { |
| 350 ConstantConstructor constantConstructor = element.constantConstructor; | 342 ConstantConstructor constantConstructor = element.constantConstructor; |
| 351 ObjectEncoder constantEncoder = | 343 ObjectEncoder constantEncoder = encoder.createObject(Key.CONSTRUCTOR); |
| 352 encoder.createObject(Key.CONSTRUCTOR); | 344 const ConstantConstructorSerializer() |
| 353 const ConstantConstructorSerializer().visit( | 345 .visit(constantConstructor, constantEncoder); |
| 354 constantConstructor, constantEncoder); | |
| 355 } | 346 } |
| 356 } | 347 } |
| 357 } | 348 } |
| 358 | 349 |
| 359 class FieldSerializer implements ElementSerializer { | 350 class FieldSerializer implements ElementSerializer { |
| 360 const FieldSerializer(); | 351 const FieldSerializer(); |
| 361 | 352 |
| 362 SerializedElementKind getSerializedKind(Element element) { | 353 SerializedElementKind getSerializedKind(Element element) { |
| 363 if (element.isField) { | 354 if (element.isField) { |
| 364 if (element.isTopLevel) return SerializedElementKind.TOPLEVEL_FIELD; | 355 if (element.isTopLevel) return SerializedElementKind.TOPLEVEL_FIELD; |
| 365 if (element.isStatic) return SerializedElementKind.STATIC_FIELD; | 356 if (element.isStatic) return SerializedElementKind.STATIC_FIELD; |
| 366 if (element.isInstanceMember) return SerializedElementKind.INSTANCE_FIELD; | 357 if (element.isInstanceMember) return SerializedElementKind.INSTANCE_FIELD; |
| 367 } | 358 } |
| 368 return null; | 359 return null; |
| 369 } | 360 } |
| 370 | 361 |
| 371 void serialize(FieldElement element, | 362 void serialize( |
| 372 ObjectEncoder encoder, | 363 FieldElement element, ObjectEncoder encoder, SerializedElementKind kind) { |
| 373 SerializedElementKind kind) { | |
| 374 encoder.setString(Key.NAME, element.name); | 364 encoder.setString(Key.NAME, element.name); |
| 375 SerializerUtil.serializePosition(element, encoder); | 365 SerializerUtil.serializePosition(element, encoder); |
| 376 encoder.setType(Key.TYPE, element.type); | 366 encoder.setType(Key.TYPE, element.type); |
| 377 encoder.setBool(Key.IS_FINAL, element.isFinal); | 367 encoder.setBool(Key.IS_FINAL, element.isFinal); |
| 378 encoder.setBool(Key.IS_CONST, element.isConst); | 368 encoder.setBool(Key.IS_CONST, element.isConst); |
| 379 if (element.isConst) { | 369 if (element.isConst) { |
| 380 ConstantExpression constant = element.constant; | 370 ConstantExpression constant = element.constant; |
| 381 encoder.setConstant(Key.CONSTANT, constant); | 371 encoder.setConstant(Key.CONSTANT, constant); |
| 382 } | 372 } |
| 383 if (kind != SerializedElementKind.TOPLEVEL_FIELD) { | 373 if (kind != SerializedElementKind.TOPLEVEL_FIELD) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 413 if (element.isSetter) { | 403 if (element.isSetter) { |
| 414 if (element.isTopLevel) return SerializedElementKind.TOPLEVEL_SETTER; | 404 if (element.isTopLevel) return SerializedElementKind.TOPLEVEL_SETTER; |
| 415 if (element.isStatic) return SerializedElementKind.STATIC_SETTER; | 405 if (element.isStatic) return SerializedElementKind.STATIC_SETTER; |
| 416 if (element.isInstanceMember) { | 406 if (element.isInstanceMember) { |
| 417 return SerializedElementKind.INSTANCE_SETTER; | 407 return SerializedElementKind.INSTANCE_SETTER; |
| 418 } | 408 } |
| 419 } | 409 } |
| 420 return null; | 410 return null; |
| 421 } | 411 } |
| 422 | 412 |
| 423 void serialize(FunctionElement element, | 413 void serialize(FunctionElement element, ObjectEncoder encoder, |
| 424 ObjectEncoder encoder, | 414 SerializedElementKind kind) { |
| 425 SerializedElementKind kind) { | |
| 426 encoder.setString(Key.NAME, element.name); | 415 encoder.setString(Key.NAME, element.name); |
| 427 SerializerUtil.serializePosition(element, encoder); | 416 SerializerUtil.serializePosition(element, encoder); |
| 428 SerializerUtil.serializeParameters(element, encoder); | 417 SerializerUtil.serializeParameters(element, encoder); |
| 429 encoder.setType(Key.TYPE, element.type); | 418 encoder.setType(Key.TYPE, element.type); |
| 430 if (element.isFunction) { | 419 if (element.isFunction) { |
| 431 encoder.setBool(Key.IS_OPERATOR, element.isOperator); | 420 encoder.setBool(Key.IS_OPERATOR, element.isOperator); |
| 432 } | 421 } |
| 433 if (element.enclosingClass != null) { | 422 if (element.enclosingClass != null) { |
| 434 encoder.setElement(Key.CLASS, element.enclosingClass); | 423 encoder.setElement(Key.CLASS, element.enclosingClass); |
| 435 } else { | 424 } else { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 448 class TypedefSerializer implements ElementSerializer { | 437 class TypedefSerializer implements ElementSerializer { |
| 449 const TypedefSerializer(); | 438 const TypedefSerializer(); |
| 450 | 439 |
| 451 SerializedElementKind getSerializedKind(Element element) { | 440 SerializedElementKind getSerializedKind(Element element) { |
| 452 if (element.isTypedef) { | 441 if (element.isTypedef) { |
| 453 return SerializedElementKind.TYPEDEF; | 442 return SerializedElementKind.TYPEDEF; |
| 454 } | 443 } |
| 455 return null; | 444 return null; |
| 456 } | 445 } |
| 457 | 446 |
| 458 void serialize(TypedefElement element, | 447 void serialize(TypedefElement element, ObjectEncoder encoder, |
| 459 ObjectEncoder encoder, | 448 SerializedElementKind kind) { |
| 460 SerializedElementKind kind) { | |
| 461 encoder.setString(Key.NAME, element.name); | 449 encoder.setString(Key.NAME, element.name); |
| 462 SerializerUtil.serializePosition(element, encoder); | 450 SerializerUtil.serializePosition(element, encoder); |
| 463 encoder.setType(Key.ALIAS, element.alias); | 451 encoder.setType(Key.ALIAS, element.alias); |
| 464 encoder.setElement(Key.LIBRARY, element.library); | 452 encoder.setElement(Key.LIBRARY, element.library); |
| 465 encoder.setTypes(Key.TYPE_VARIABLES, element.typeVariables); | 453 encoder.setTypes(Key.TYPE_VARIABLES, element.typeVariables); |
| 466 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); | 454 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); |
| 467 } | 455 } |
| 468 } | 456 } |
| 469 | 457 |
| 470 class TypeVariableSerializer implements ElementSerializer { | 458 class TypeVariableSerializer implements ElementSerializer { |
| 471 const TypeVariableSerializer(); | 459 const TypeVariableSerializer(); |
| 472 | 460 |
| 473 SerializedElementKind getSerializedKind(Element element) { | 461 SerializedElementKind getSerializedKind(Element element) { |
| 474 if (element.isTypeVariable) { | 462 if (element.isTypeVariable) { |
| 475 return SerializedElementKind.TYPEVARIABLE; | 463 return SerializedElementKind.TYPEVARIABLE; |
| 476 } | 464 } |
| 477 return null; | 465 return null; |
| 478 } | 466 } |
| 479 | 467 |
| 480 void serialize(TypeVariableElement element, | 468 void serialize(TypeVariableElement element, ObjectEncoder encoder, |
| 481 ObjectEncoder encoder, | 469 SerializedElementKind kind) { |
| 482 SerializedElementKind kind) { | |
| 483 encoder.setElement(Key.TYPE_DECLARATION, element.typeDeclaration); | 470 encoder.setElement(Key.TYPE_DECLARATION, element.typeDeclaration); |
| 484 encoder.setString(Key.NAME, element.name); | 471 encoder.setString(Key.NAME, element.name); |
| 485 SerializerUtil.serializePosition(element, encoder); | 472 SerializerUtil.serializePosition(element, encoder); |
| 486 encoder.setType(Key.TYPE, element.type); | 473 encoder.setType(Key.TYPE, element.type); |
| 487 encoder.setInt(Key.INDEX, element.index); | 474 encoder.setInt(Key.INDEX, element.index); |
| 488 encoder.setType(Key.BOUND, element.bound); | 475 encoder.setType(Key.BOUND, element.bound); |
| 489 } | 476 } |
| 490 } | 477 } |
| 491 | 478 |
| 492 class ParameterSerializer implements ElementSerializer { | 479 class ParameterSerializer implements ElementSerializer { |
| 493 const ParameterSerializer(); | 480 const ParameterSerializer(); |
| 494 | 481 |
| 495 SerializedElementKind getSerializedKind(Element element) { | 482 SerializedElementKind getSerializedKind(Element element) { |
| 496 if (element.isParameter) { | 483 if (element.isParameter) { |
| 497 return SerializedElementKind.PARAMETER; | 484 return SerializedElementKind.PARAMETER; |
| 498 } else if (element.isInitializingFormal) { | 485 } else if (element.isInitializingFormal) { |
| 499 return SerializedElementKind.INITIALIZING_FORMAL; | 486 return SerializedElementKind.INITIALIZING_FORMAL; |
| 500 } | 487 } |
| 501 return null; | 488 return null; |
| 502 } | 489 } |
| 503 | 490 |
| 504 void serialize(ParameterElement element, | 491 void serialize(ParameterElement element, ObjectEncoder encoder, |
| 505 ObjectEncoder encoder, | 492 SerializedElementKind kind) { |
| 506 SerializedElementKind kind) { | |
| 507 encoder.setElement(Key.FUNCTION, element.functionDeclaration); | 493 encoder.setElement(Key.FUNCTION, element.functionDeclaration); |
| 508 encoder.setString(Key.NAME, element.name); | 494 encoder.setString(Key.NAME, element.name); |
| 509 SerializerUtil.serializePosition(element, encoder); | 495 SerializerUtil.serializePosition(element, encoder); |
| 510 encoder.setType(Key.TYPE, element.type); | 496 encoder.setType(Key.TYPE, element.type); |
| 511 encoder.setBool(Key.IS_OPTIONAL, element.isOptional); | 497 encoder.setBool(Key.IS_OPTIONAL, element.isOptional); |
| 512 encoder.setBool(Key.IS_NAMED, element.isNamed); | 498 encoder.setBool(Key.IS_NAMED, element.isNamed); |
| 513 if (element.isOptional) { | 499 if (element.isOptional) { |
| 514 encoder.setConstant(Key.CONSTANT, element.constant); | 500 encoder.setConstant(Key.CONSTANT, element.constant); |
| 515 } | 501 } |
| 516 if (element.isInitializingFormal) { | 502 if (element.isInitializingFormal) { |
| 517 InitializingFormalElement initializingFormal = element; | 503 InitializingFormalElement initializingFormal = element; |
| 518 encoder.setElement(Key.FIELD, initializingFormal.fieldElement); | 504 encoder.setElement(Key.FIELD, initializingFormal.fieldElement); |
| 519 } | 505 } |
| 520 } | 506 } |
| 521 } | 507 } |
| 522 | 508 |
| 523 class ImportSerializer implements ElementSerializer { | 509 class ImportSerializer implements ElementSerializer { |
| 524 const ImportSerializer(); | 510 const ImportSerializer(); |
| 525 | 511 |
| 526 SerializedElementKind getSerializedKind(Element element) { | 512 SerializedElementKind getSerializedKind(Element element) { |
| 527 if (element.isImport) { | 513 if (element.isImport) { |
| 528 return SerializedElementKind.IMPORT; | 514 return SerializedElementKind.IMPORT; |
| 529 } | 515 } |
| 530 return null; | 516 return null; |
| 531 } | 517 } |
| 532 | 518 |
| 533 void serialize(ImportElement element, | 519 void serialize(ImportElement element, ObjectEncoder encoder, |
| 534 ObjectEncoder encoder, | 520 SerializedElementKind kind) { |
| 535 SerializedElementKind kind) { | |
| 536 encoder.setElement(Key.LIBRARY, element.library); | 521 encoder.setElement(Key.LIBRARY, element.library); |
| 537 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); | 522 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); |
| 538 encoder.setElement(Key.LIBRARY_DEPENDENCY, element.importedLibrary); | 523 encoder.setElement(Key.LIBRARY_DEPENDENCY, element.importedLibrary); |
| 539 if (element.prefix != null) { | 524 if (element.prefix != null) { |
| 540 encoder.setElement(Key.PREFIX, element.prefix); | 525 encoder.setElement(Key.PREFIX, element.prefix); |
| 541 } | 526 } |
| 542 encoder.setBool(Key.IS_DEFERRED, element.isDeferred); | 527 encoder.setBool(Key.IS_DEFERRED, element.isDeferred); |
| 543 // TODO(johnniwinther): What is the base for the URI? | 528 // TODO(johnniwinther): What is the base for the URI? |
| 544 encoder.setUri(Key.URI, element.uri, element.uri); | 529 encoder.setUri(Key.URI, element.uri, element.uri); |
| 545 } | 530 } |
| 546 } | 531 } |
| 547 | 532 |
| 548 class ExportSerializer implements ElementSerializer { | 533 class ExportSerializer implements ElementSerializer { |
| 549 const ExportSerializer(); | 534 const ExportSerializer(); |
| 550 | 535 |
| 551 SerializedElementKind getSerializedKind(Element element) { | 536 SerializedElementKind getSerializedKind(Element element) { |
| 552 if (element.isExport) { | 537 if (element.isExport) { |
| 553 return SerializedElementKind.EXPORT; | 538 return SerializedElementKind.EXPORT; |
| 554 } | 539 } |
| 555 return null; | 540 return null; |
| 556 } | 541 } |
| 557 | 542 |
| 558 void serialize(ExportElement element, | 543 void serialize(ExportElement element, ObjectEncoder encoder, |
| 559 ObjectEncoder encoder, | 544 SerializedElementKind kind) { |
| 560 SerializedElementKind kind) { | |
| 561 encoder.setElement(Key.LIBRARY, element.library); | 545 encoder.setElement(Key.LIBRARY, element.library); |
| 562 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); | 546 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); |
| 563 encoder.setElement(Key.LIBRARY_DEPENDENCY, element.exportedLibrary); | 547 encoder.setElement(Key.LIBRARY_DEPENDENCY, element.exportedLibrary); |
| 564 // TODO(johnniwinther): What is the base for the URI? | 548 // TODO(johnniwinther): What is the base for the URI? |
| 565 encoder.setUri(Key.URI, element.uri, element.uri); | 549 encoder.setUri(Key.URI, element.uri, element.uri); |
| 566 } | 550 } |
| 567 } | 551 } |
| 568 | 552 |
| 569 class PrefixSerializer implements ElementSerializer { | 553 class PrefixSerializer implements ElementSerializer { |
| 570 const PrefixSerializer(); | 554 const PrefixSerializer(); |
| 571 | 555 |
| 572 SerializedElementKind getSerializedKind(Element element) { | 556 SerializedElementKind getSerializedKind(Element element) { |
| 573 if (element.isPrefix) { | 557 if (element.isPrefix) { |
| 574 return SerializedElementKind.PREFIX; | 558 return SerializedElementKind.PREFIX; |
| 575 } | 559 } |
| 576 return null; | 560 return null; |
| 577 } | 561 } |
| 578 | 562 |
| 579 void serialize(PrefixElement element, | 563 void serialize(PrefixElement element, ObjectEncoder encoder, |
| 580 ObjectEncoder encoder, | 564 SerializedElementKind kind) { |
| 581 SerializedElementKind kind) { | |
| 582 encoder.setString(Key.NAME, element.name); | 565 encoder.setString(Key.NAME, element.name); |
| 583 encoder.setElement(Key.LIBRARY, element.library); | 566 encoder.setElement(Key.LIBRARY, element.library); |
| 584 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); | 567 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); |
| 585 if (element.deferredImport != null) { | 568 if (element.deferredImport != null) { |
| 586 encoder.setElement(Key.IMPORT, element.deferredImport); | 569 encoder.setElement(Key.IMPORT, element.deferredImport); |
| 587 } | 570 } |
| 588 encoder.setBool(Key.IS_DEFERRED, element.isDeferred); | 571 encoder.setBool(Key.IS_DEFERRED, element.isDeferred); |
| 589 } | 572 } |
| 590 } | 573 } |
| 591 | 574 |
| 592 /// Utility class for deserializing [Element]s. | 575 /// Utility class for deserializing [Element]s. |
| 593 /// | 576 /// |
| 594 /// This is used by the [Deserializer]. | 577 /// This is used by the [Deserializer]. |
| 595 class ElementDeserializer { | 578 class ElementDeserializer { |
| 596 | |
| 597 /// Deserializes an [Element] from an [ObjectDecoder]. | 579 /// Deserializes an [Element] from an [ObjectDecoder]. |
| 598 /// | 580 /// |
| 599 /// The class is called from the [Deserializer] when an [Element] | 581 /// The class is called from the [Deserializer] when an [Element] |
| 600 /// needs deserialization. The [ObjectDecoder] ensures that any [Element], | 582 /// needs deserialization. The [ObjectDecoder] ensures that any [Element], |
| 601 /// [DartType], and [ConstantExpression] that the deserialized [Element] | 583 /// [DartType], and [ConstantExpression] that the deserialized [Element] |
| 602 /// depends upon are available. | 584 /// depends upon are available. |
| 603 static Element deserialize( | 585 static Element deserialize( |
| 604 ObjectDecoder decoder, | 586 ObjectDecoder decoder, SerializedElementKind elementKind) { |
| 605 SerializedElementKind elementKind) { | |
| 606 switch (elementKind) { | 587 switch (elementKind) { |
| 607 case SerializedElementKind.LIBRARY: | 588 case SerializedElementKind.LIBRARY: |
| 608 return new LibraryElementZ(decoder); | 589 return new LibraryElementZ(decoder); |
| 609 case SerializedElementKind.COMPILATION_UNIT: | 590 case SerializedElementKind.COMPILATION_UNIT: |
| 610 return new CompilationUnitElementZ(decoder); | 591 return new CompilationUnitElementZ(decoder); |
| 611 case SerializedElementKind.CLASS: | 592 case SerializedElementKind.CLASS: |
| 612 return new ClassElementZ(decoder); | 593 return new ClassElementZ(decoder); |
| 613 case SerializedElementKind.ENUM: | 594 case SerializedElementKind.ENUM: |
| 614 return new EnumClassElementZ(decoder); | 595 return new EnumClassElementZ(decoder); |
| 615 case SerializedElementKind.NAMED_MIXIN_APPLICATION: | 596 case SerializedElementKind.NAMED_MIXIN_APPLICATION: |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 659 case SerializedElementKind.PREFIX: | 640 case SerializedElementKind.PREFIX: |
| 660 return new PrefixElementZ(decoder); | 641 return new PrefixElementZ(decoder); |
| 661 case SerializedElementKind.EXTERNAL_LIBRARY: | 642 case SerializedElementKind.EXTERNAL_LIBRARY: |
| 662 case SerializedElementKind.EXTERNAL_LIBRARY_MEMBER: | 643 case SerializedElementKind.EXTERNAL_LIBRARY_MEMBER: |
| 663 case SerializedElementKind.EXTERNAL_STATIC_MEMBER: | 644 case SerializedElementKind.EXTERNAL_STATIC_MEMBER: |
| 664 case SerializedElementKind.EXTERNAL_CONSTRUCTOR: | 645 case SerializedElementKind.EXTERNAL_CONSTRUCTOR: |
| 665 break; | 646 break; |
| 666 } | 647 } |
| 667 throw new UnsupportedError("Unexpected element kind '${elementKind}."); | 648 throw new UnsupportedError("Unexpected element kind '${elementKind}."); |
| 668 } | 649 } |
| 669 } | 650 } |
| OLD | NEW |