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 serialization.elements; | 5 library serialization.elements; |
6 | 6 |
7 import 'dart:convert'; | 7 import 'dart:convert'; |
8 | 8 |
9 import 'package:analyzer/dart/element/element.dart'; | 9 import 'package:analyzer/dart/element/element.dart'; |
10 import 'package:analyzer/dart/element/type.dart'; | 10 import 'package:analyzer/dart/element/type.dart'; |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 */ | 239 */ |
240 int numSlots = 0; | 240 int numSlots = 0; |
241 | 241 |
242 /** | 242 /** |
243 * List of closures which should be invoked at the end of serialization of a | 243 * List of closures which should be invoked at the end of serialization of a |
244 * compilation unit, to produce [LinkedUnit.types]. | 244 * compilation unit, to produce [LinkedUnit.types]. |
245 */ | 245 */ |
246 final List<_SerializeTypeRef> deferredLinkedTypes = <_SerializeTypeRef>[]; | 246 final List<_SerializeTypeRef> deferredLinkedTypes = <_SerializeTypeRef>[]; |
247 | 247 |
248 /** | 248 /** |
| 249 * List which should be stored in [LinkedUnit.constCycles]. |
| 250 */ |
| 251 final List<int> constCycles = <int>[]; |
| 252 |
| 253 /** |
249 * Index into the "references table" representing an unresolved reference, if | 254 * Index into the "references table" representing an unresolved reference, if |
250 * such an index exists. `null` if no such entry has been made in the | 255 * such an index exists. `null` if no such entry has been made in the |
251 * references table yet. | 256 * references table yet. |
252 */ | 257 */ |
253 int unresolvedReferenceIndex = null; | 258 int unresolvedReferenceIndex = null; |
254 | 259 |
255 /** | 260 /** |
256 * Index into the "references table" representing the "bottom" type, if such | 261 * Index into the "references table" representing the "bottom" type, if such |
257 * an index exists. `null` if no such entry has been made in the references | 262 * an index exists. `null` if no such entry has been made in the references |
258 * table yet. | 263 * table yet. |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 } | 392 } |
388 } | 393 } |
389 unlinkedUnit.variables = variables; | 394 unlinkedUnit.variables = variables; |
390 unlinkedUnit.references = unlinkedReferences; | 395 unlinkedUnit.references = unlinkedReferences; |
391 linkedUnit.references = linkedReferences; | 396 linkedUnit.references = linkedReferences; |
392 unitUri = compilationUnit.source.uri.toString(); | 397 unitUri = compilationUnit.source.uri.toString(); |
393 } | 398 } |
394 | 399 |
395 /** | 400 /** |
396 * Create the [LinkedUnit.types] table based on deferred types that were | 401 * Create the [LinkedUnit.types] table based on deferred types that were |
397 * found during [addCompilationUnitElements]. | 402 * found during [addCompilationUnitElements]. Also populate |
| 403 * [LinkedUnit.constCycles]. |
398 */ | 404 */ |
399 void createLinkedTypes() { | 405 void createLinkedInfo() { |
400 buildingLinkedReferences = true; | 406 buildingLinkedReferences = true; |
401 linkedUnit.types = deferredLinkedTypes | 407 linkedUnit.types = deferredLinkedTypes |
402 .map((_SerializeTypeRef closure) => closure()) | 408 .map((_SerializeTypeRef closure) => closure()) |
403 .toList(); | 409 .toList(); |
| 410 linkedUnit.constCycles = constCycles; |
404 buildingLinkedReferences = false; | 411 buildingLinkedReferences = false; |
405 } | 412 } |
406 | 413 |
407 /** | 414 /** |
408 * Compute the appropriate De Bruijn index to represent the given type | 415 * Compute the appropriate De Bruijn index to represent the given type |
409 * parameter [type]. | 416 * parameter [type]. |
410 */ | 417 */ |
411 int findTypeParameterIndex(TypeParameterType type, Element context) { | 418 int findTypeParameterIndex(TypeParameterType type, Element context) { |
412 Element originalContext = context; | 419 Element originalContext = context; |
413 int index = 0; | 420 int index = 0; |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
699 unit: typeLinkedRef.unit, prefixReference: typeId); | 706 unit: typeLinkedRef.unit, prefixReference: typeId); |
700 b.redirectedConstructor = new EntityRefBuilder( | 707 b.redirectedConstructor = new EntityRefBuilder( |
701 reference: refId, typeArguments: typeRef.typeArguments); | 708 reference: refId, typeArguments: typeRef.typeArguments); |
702 } else { | 709 } else { |
703 b.redirectedConstructor = typeRef; | 710 b.redirectedConstructor = typeRef; |
704 } | 711 } |
705 } else { | 712 } else { |
706 b.redirectedConstructorName = redirectedConstructor.name; | 713 b.redirectedConstructorName = redirectedConstructor.name; |
707 } | 714 } |
708 } | 715 } |
709 if (executableElement.isConst && | 716 if (executableElement.isConst) { |
710 executableElement.constantInitializers != null) { | 717 b.constCycleSlot = storeConstCycle(!executableElement.isCycleFree); |
711 Set<String> constructorParameterNames = | 718 if (executableElement.constantInitializers != null) { |
712 executableElement.parameters.map((p) => p.name).toSet(); | 719 Set<String> constructorParameterNames = |
713 b.constantInitializers = executableElement.constantInitializers | 720 executableElement.parameters.map((p) => p.name).toSet(); |
714 .map((ConstructorInitializer initializer) => | 721 b.constantInitializers = executableElement.constantInitializers |
715 serializeConstructorInitializer( | 722 .map((ConstructorInitializer initializer) => |
716 initializer, | 723 serializeConstructorInitializer( |
717 (expr) => | 724 initializer, |
718 serializeConstExpr(expr, constructorParameterNames))) | 725 (expr) => |
719 .toList(); | 726 serializeConstExpr(expr, constructorParameterNames))) |
| 727 .toList(); |
| 728 } |
720 } | 729 } |
721 } else { | 730 } else { |
722 b.kind = UnlinkedExecutableKind.functionOrMethod; | 731 b.kind = UnlinkedExecutableKind.functionOrMethod; |
723 } | 732 } |
724 b.isAbstract = executableElement.isAbstract; | 733 b.isAbstract = executableElement.isAbstract; |
725 b.isStatic = executableElement.isStatic && | 734 b.isStatic = executableElement.isStatic && |
726 executableElement.enclosingElement is ClassElement; | 735 executableElement.enclosingElement is ClassElement; |
727 b.isExternal = executableElement.isExternal; | 736 b.isExternal = executableElement.isExternal; |
728 b.documentationComment = serializeDocumentation(executableElement); | 737 b.documentationComment = serializeDocumentation(executableElement); |
729 b.annotations = serializeAnnotations(executableElement); | 738 b.annotations = serializeAnnotations(executableElement); |
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1097 * [LinkedUnit.types]. | 1106 * [LinkedUnit.types]. |
1098 * | 1107 * |
1099 * [context] is the element within which the slot id will appear; this is | 1108 * [context] is the element within which the slot id will appear; this is |
1100 * used to serialize type parameters. | 1109 * used to serialize type parameters. |
1101 */ | 1110 */ |
1102 int storeInferredType(DartType type, Element context) { | 1111 int storeInferredType(DartType type, Element context) { |
1103 return storeLinkedType(type.isDynamic ? null : type, context); | 1112 return storeLinkedType(type.isDynamic ? null : type, context); |
1104 } | 1113 } |
1105 | 1114 |
1106 /** | 1115 /** |
| 1116 * Create a new slot id and return it. If [hasCycle] is `true`, arrange for |
| 1117 * the slot id to be included in [LinkedUnit.constCycles]. |
| 1118 */ |
| 1119 int storeConstCycle(bool hasCycle) { |
| 1120 int slot = ++numSlots; |
| 1121 if (hasCycle) { |
| 1122 constCycles.add(slot); |
| 1123 } |
| 1124 return slot; |
| 1125 } |
| 1126 |
| 1127 /** |
1107 * Create a slot id for the given [type] (which may be either a propagated | 1128 * Create a slot id for the given [type] (which may be either a propagated |
1108 * type or an inferred type). If [type] is not `null`, it is stored in | 1129 * type or an inferred type). If [type] is not `null`, it is stored in |
1109 * [linkedTypes] so that once the compilation unit has been fully visited, | 1130 * [linkedTypes] so that once the compilation unit has been fully visited, |
1110 * it will be serialized to [LinkedUnit.types]. | 1131 * it will be serialized to [LinkedUnit.types]. |
1111 * | 1132 * |
1112 * [context] is the element within which the slot id will appear; this is | 1133 * [context] is the element within which the slot id will appear; this is |
1113 * used to serialize type parameters. | 1134 * used to serialize type parameters. |
1114 */ | 1135 */ |
1115 int storeLinkedType(DartType type, Element context) { | 1136 int storeLinkedType(DartType type, Element context) { |
1116 int slot = ++numSlots; | 1137 int slot = ++numSlots; |
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1515 in compilationUnitSerializers) { | 1536 in compilationUnitSerializers) { |
1516 compilationUnitSerializer.addCompilationUnitElements(); | 1537 compilationUnitSerializer.addCompilationUnitElements(); |
1517 } | 1538 } |
1518 pb.units = compilationUnitSerializers | 1539 pb.units = compilationUnitSerializers |
1519 .map((_CompilationUnitSerializer s) => s.linkedUnit) | 1540 .map((_CompilationUnitSerializer s) => s.linkedUnit) |
1520 .toList(); | 1541 .toList(); |
1521 pb.dependencies = dependencies; | 1542 pb.dependencies = dependencies; |
1522 pb.numPrelinkedDependencies = dependencies.length; | 1543 pb.numPrelinkedDependencies = dependencies.length; |
1523 for (_CompilationUnitSerializer compilationUnitSerializer | 1544 for (_CompilationUnitSerializer compilationUnitSerializer |
1524 in compilationUnitSerializers) { | 1545 in compilationUnitSerializers) { |
1525 compilationUnitSerializer.createLinkedTypes(); | 1546 compilationUnitSerializer.createLinkedInfo(); |
1526 } | 1547 } |
1527 pb.importDependencies = linkedImports; | 1548 pb.importDependencies = linkedImports; |
1528 List<String> exportedNames = | 1549 List<String> exportedNames = |
1529 libraryElement.exportNamespace.definedNames.keys.toList(); | 1550 libraryElement.exportNamespace.definedNames.keys.toList(); |
1530 exportedNames.sort(); | 1551 exportedNames.sort(); |
1531 List<LinkedExportNameBuilder> exportNames = <LinkedExportNameBuilder>[]; | 1552 List<LinkedExportNameBuilder> exportNames = <LinkedExportNameBuilder>[]; |
1532 for (String name in exportedNames) { | 1553 for (String name in exportedNames) { |
1533 if (libraryElement.publicNamespace.definedNames.containsKey(name)) { | 1554 if (libraryElement.publicNamespace.definedNames.containsKey(name)) { |
1534 continue; | 1555 continue; |
1535 } | 1556 } |
1536 Element element = libraryElement.exportNamespace.get(name); | 1557 Element element = libraryElement.exportNamespace.get(name); |
1537 LibraryElement dependentLibrary = element.library; | 1558 LibraryElement dependentLibrary = element.library; |
1538 CompilationUnitElement unitElement = | 1559 CompilationUnitElement unitElement = |
1539 element.getAncestor((Element e) => e is CompilationUnitElement); | 1560 element.getAncestor((Element e) => e is CompilationUnitElement); |
1540 int unit = dependentLibrary.units.indexOf(unitElement); | 1561 int unit = dependentLibrary.units.indexOf(unitElement); |
1541 assert(unit != -1); | 1562 assert(unit != -1); |
1542 ReferenceKind kind = _getReferenceKind(element); | 1563 ReferenceKind kind = _getReferenceKind(element); |
1543 exportNames.add(new LinkedExportNameBuilder( | 1564 exportNames.add(new LinkedExportNameBuilder( |
1544 name: name, | 1565 name: name, |
1545 dependency: serializeDependency(dependentLibrary), | 1566 dependency: serializeDependency(dependentLibrary), |
1546 unit: unit, | 1567 unit: unit, |
1547 kind: kind)); | 1568 kind: kind)); |
1548 } | 1569 } |
1549 pb.exportNames = exportNames; | 1570 pb.exportNames = exportNames; |
1550 return pb; | 1571 return pb; |
1551 } | 1572 } |
1552 } | 1573 } |
OLD | NEW |