| 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.supertype != null) { | 283 if (element.supertype != null) { |
| 260 encoder.setType(Key.SUPERTYPE, element.supertype); | 284 encoder.setType(Key.SUPERTYPE, element.supertype); |
| 261 } | 285 } |
| 262 // TODO(johnniwinther): Make [OrderedTypeSet] easier to (de)serialize. | 286 // TODO(johnniwinther): Make [OrderedTypeSet] easier to (de)serialize. |
| 263 ObjectEncoder supertypes = encoder.createObject(Key.SUPERTYPES); | 287 ObjectEncoder supertypes = encoder.createObject(Key.SUPERTYPES); |
| 264 supertypes.setTypes(Key.TYPES, | 288 supertypes.setTypes(Key.TYPES, |
| 265 element.allSupertypesAndSelf.types.toList()); | 289 element.allSupertypesAndSelf.types.toList()); |
| 266 supertypes.setTypes(Key.SUPERTYPES, | 290 supertypes.setTypes(Key.SUPERTYPES, |
| 267 element.allSupertypesAndSelf.supertypes.toList()); | 291 element.allSupertypesAndSelf.supertypes.toList()); |
| 268 supertypes.setInts(Key.OFFSETS, element.allSupertypesAndSelf.levelOffsets); | 292 supertypes.setInts(Key.OFFSETS, element.allSupertypesAndSelf.levelOffsets); |
| 269 encoder.setTypes(Key.INTERFACES, element.interfaces.toList()); | 293 encoder.setTypes(Key.INTERFACES, element.interfaces.toList()); |
| 270 SerializerUtil.serializeMembers(element, encoder); | 294 SerializerUtil.serializeMembers(getMembers(element), encoder); |
| 295 encoder.setBool(Key.IS_PROXY, element.isProxy); |
| 271 if (kind == SerializedElementKind.ENUM) { | 296 if (kind == SerializedElementKind.ENUM) { |
| 272 EnumClassElement enumClass = element; | 297 EnumClassElement enumClass = element; |
| 273 encoder.setElements(Key.FIELDS, enumClass.enumValues); | 298 encoder.setElements(Key.FIELDS, enumClass.enumValues); |
| 274 } | 299 } |
| 300 |
| 275 } | 301 } |
| 276 } | 302 } |
| 277 | 303 |
| 278 class ConstructorSerializer implements ElementSerializer { | 304 class ConstructorSerializer implements ElementSerializer { |
| 279 const ConstructorSerializer(); | 305 const ConstructorSerializer(); |
| 280 | 306 |
| 281 SerializedElementKind getSerializedKind(Element element) { | 307 SerializedElementKind getSerializedKind(Element element) { |
| 282 if (element.isGenerativeConstructor) { | 308 if (element.isGenerativeConstructor) { |
| 283 return SerializedElementKind.GENERATIVE_CONSTRUCTOR; | 309 return SerializedElementKind.GENERATIVE_CONSTRUCTOR; |
| 284 } else if (element.isFactoryConstructor) { | 310 } else if (element.isFactoryConstructor) { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 class FunctionSerializer implements ElementSerializer { | 371 class FunctionSerializer implements ElementSerializer { |
| 346 const FunctionSerializer(); | 372 const FunctionSerializer(); |
| 347 | 373 |
| 348 SerializedElementKind getSerializedKind(Element element) { | 374 SerializedElementKind getSerializedKind(Element element) { |
| 349 if (element.isFunction) { | 375 if (element.isFunction) { |
| 350 if (element.isTopLevel) return SerializedElementKind.TOPLEVEL_FUNCTION; | 376 if (element.isTopLevel) return SerializedElementKind.TOPLEVEL_FUNCTION; |
| 351 if (element.isStatic) return SerializedElementKind.STATIC_FUNCTION; | 377 if (element.isStatic) return SerializedElementKind.STATIC_FUNCTION; |
| 352 if (element.isInstanceMember) { | 378 if (element.isInstanceMember) { |
| 353 return SerializedElementKind.INSTANCE_FUNCTION; | 379 return SerializedElementKind.INSTANCE_FUNCTION; |
| 354 } | 380 } |
| 381 if (element.isLocal) { |
| 382 return SerializedElementKind.LOCAL_FUNCTION; |
| 383 } |
| 355 } | 384 } |
| 356 if (element.isGetter) { | 385 if (element.isGetter) { |
| 357 if (element.isTopLevel) return SerializedElementKind.TOPLEVEL_GETTER; | 386 if (element.isTopLevel) return SerializedElementKind.TOPLEVEL_GETTER; |
| 358 if (element.isStatic) return SerializedElementKind.STATIC_GETTER; | 387 if (element.isStatic) return SerializedElementKind.STATIC_GETTER; |
| 359 if (element.isInstanceMember) { | 388 if (element.isInstanceMember) { |
| 360 return SerializedElementKind.INSTANCE_GETTER; | 389 return SerializedElementKind.INSTANCE_GETTER; |
| 361 } | 390 } |
| 362 } | 391 } |
| 363 if (element.isSetter) { | 392 if (element.isSetter) { |
| 364 if (element.isTopLevel) return SerializedElementKind.TOPLEVEL_SETTER; | 393 if (element.isTopLevel) return SerializedElementKind.TOPLEVEL_SETTER; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 380 if (element.isFunction) { | 409 if (element.isFunction) { |
| 381 encoder.setBool(Key.IS_OPERATOR, element.isOperator); | 410 encoder.setBool(Key.IS_OPERATOR, element.isOperator); |
| 382 } | 411 } |
| 383 if (element.enclosingClass != null) { | 412 if (element.enclosingClass != null) { |
| 384 encoder.setElement(Key.CLASS, element.enclosingClass); | 413 encoder.setElement(Key.CLASS, element.enclosingClass); |
| 385 } else { | 414 } else { |
| 386 encoder.setElement(Key.LIBRARY, element.library); | 415 encoder.setElement(Key.LIBRARY, element.library); |
| 387 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); | 416 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); |
| 388 } | 417 } |
| 389 encoder.setBool(Key.IS_EXTERNAL, element.isExternal); | 418 encoder.setBool(Key.IS_EXTERNAL, element.isExternal); |
| 419 if (element.isLocal) { |
| 420 LocalFunctionElement localFunction = element; |
| 421 encoder.setElement( |
| 422 Key.EXECUTABLE_CONTEXT, localFunction.executableContext); |
| 423 } |
| 390 } | 424 } |
| 391 } | 425 } |
| 392 | 426 |
| 393 class TypedefSerializer implements ElementSerializer { | 427 class TypedefSerializer implements ElementSerializer { |
| 394 const TypedefSerializer(); | 428 const TypedefSerializer(); |
| 395 | 429 |
| 396 SerializedElementKind getSerializedKind(Element element) { | 430 SerializedElementKind getSerializedKind(Element element) { |
| 397 if (element.isTypedef) { | 431 if (element.isTypedef) { |
| 398 return SerializedElementKind.TYPEDEF; | 432 return SerializedElementKind.TYPEDEF; |
| 399 } | 433 } |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 566 case SerializedElementKind.GENERATIVE_CONSTRUCTOR: | 600 case SerializedElementKind.GENERATIVE_CONSTRUCTOR: |
| 567 return new GenerativeConstructorElementZ(decoder); | 601 return new GenerativeConstructorElementZ(decoder); |
| 568 case SerializedElementKind.FACTORY_CONSTRUCTOR: | 602 case SerializedElementKind.FACTORY_CONSTRUCTOR: |
| 569 return new FactoryConstructorElementZ(decoder); | 603 return new FactoryConstructorElementZ(decoder); |
| 570 case SerializedElementKind.TOPLEVEL_FUNCTION: | 604 case SerializedElementKind.TOPLEVEL_FUNCTION: |
| 571 return new TopLevelFunctionElementZ(decoder); | 605 return new TopLevelFunctionElementZ(decoder); |
| 572 case SerializedElementKind.STATIC_FUNCTION: | 606 case SerializedElementKind.STATIC_FUNCTION: |
| 573 return new StaticFunctionElementZ(decoder); | 607 return new StaticFunctionElementZ(decoder); |
| 574 case SerializedElementKind.INSTANCE_FUNCTION: | 608 case SerializedElementKind.INSTANCE_FUNCTION: |
| 575 return new InstanceFunctionElementZ(decoder); | 609 return new InstanceFunctionElementZ(decoder); |
| 610 case SerializedElementKind.LOCAL_FUNCTION: |
| 611 return new LocalFunctionElementZ(decoder); |
| 576 case SerializedElementKind.TOPLEVEL_GETTER: | 612 case SerializedElementKind.TOPLEVEL_GETTER: |
| 577 return new TopLevelGetterElementZ(decoder); | 613 return new TopLevelGetterElementZ(decoder); |
| 578 case SerializedElementKind.STATIC_GETTER: | 614 case SerializedElementKind.STATIC_GETTER: |
| 579 return new StaticGetterElementZ(decoder); | 615 return new StaticGetterElementZ(decoder); |
| 580 case SerializedElementKind.INSTANCE_GETTER: | 616 case SerializedElementKind.INSTANCE_GETTER: |
| 581 return new InstanceGetterElementZ(decoder); | 617 return new InstanceGetterElementZ(decoder); |
| 582 case SerializedElementKind.TOPLEVEL_SETTER: | 618 case SerializedElementKind.TOPLEVEL_SETTER: |
| 583 return new TopLevelSetterElementZ(decoder); | 619 return new TopLevelSetterElementZ(decoder); |
| 584 case SerializedElementKind.STATIC_SETTER: | 620 case SerializedElementKind.STATIC_SETTER: |
| 585 return new StaticSetterElementZ(decoder); | 621 return new StaticSetterElementZ(decoder); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 596 case SerializedElementKind.IMPORT: | 632 case SerializedElementKind.IMPORT: |
| 597 return new ImportElementZ(decoder); | 633 return new ImportElementZ(decoder); |
| 598 case SerializedElementKind.EXPORT: | 634 case SerializedElementKind.EXPORT: |
| 599 return new ExportElementZ(decoder); | 635 return new ExportElementZ(decoder); |
| 600 case SerializedElementKind.PREFIX: | 636 case SerializedElementKind.PREFIX: |
| 601 return new PrefixElementZ(decoder); | 637 return new PrefixElementZ(decoder); |
| 602 } | 638 } |
| 603 throw new UnsupportedError("Unexpected element kind '${elementKind}."); | 639 throw new UnsupportedError("Unexpected element kind '${elementKind}."); |
| 604 } | 640 } |
| 605 } | 641 } |
| OLD | NEW |