Chromium Code Reviews| 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 'package:analyzer/dart/element/element.dart'; | 7 import 'package:analyzer/dart/element/element.dart'; |
| 8 import 'package:analyzer/dart/element/type.dart'; | 8 import 'package:analyzer/dart/element/type.dart'; |
| 9 import 'package:analyzer/src/dart/element/type.dart'; | 9 import 'package:analyzer/src/dart/element/type.dart'; |
| 10 import 'package:analyzer/src/generated/resolver.dart'; | 10 import 'package:analyzer/src/generated/resolver.dart'; |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 131 * closure of exports. | 131 * closure of exports. |
| 132 */ | 132 */ |
| 133 final Set<LibraryElement> librariesAddedToTransitiveExportClosure = | 133 final Set<LibraryElement> librariesAddedToTransitiveExportClosure = |
| 134 new Set<LibraryElement>(); | 134 new Set<LibraryElement>(); |
| 135 | 135 |
| 136 /** | 136 /** |
| 137 * [BuilderContext] used to serialize the output summary. | 137 * [BuilderContext] used to serialize the output summary. |
| 138 */ | 138 */ |
| 139 final BuilderContext ctx; | 139 final BuilderContext ctx; |
| 140 | 140 |
| 141 /** | |
| 142 * Map from imported element to the prefix which may be used to refer to that | |
| 143 * element, or null if no prefix is needed. | |
| 144 */ | |
| 145 final Map<Element, PrefixElement> prefixMap = <Element, PrefixElement>{}; | |
| 146 | |
| 141 _LibrarySerializer(this.ctx, this.libraryElement, this.typeProvider) { | 147 _LibrarySerializer(this.ctx, this.libraryElement, this.typeProvider) { |
| 142 dependencies.add(encodePrelinkedDependency(ctx)); | 148 dependencies.add(encodePrelinkedDependency(ctx)); |
| 143 dependencyMap[libraryElement] = 0; | 149 dependencyMap[libraryElement] = 0; |
| 144 } | 150 } |
| 145 | 151 |
| 146 /** | 152 /** |
| 147 * Retrieve the library element for `dart:core`. | 153 * Retrieve the library element for `dart:core`. |
| 148 */ | 154 */ |
| 149 LibraryElement get coreLibrary => typeProvider.objectType.element.library; | 155 LibraryElement get coreLibrary => typeProvider.objectType.element.library; |
| 150 | 156 |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 269 if (librariesAddedToTransitiveExportClosure.add(exportedLibrary)) { | 275 if (librariesAddedToTransitiveExportClosure.add(exportedLibrary)) { |
| 270 serializeDependency(exportedLibrary); | 276 serializeDependency(exportedLibrary); |
| 271 for (LibraryElement transitiveExport | 277 for (LibraryElement transitiveExport |
| 272 in exportedLibrary.exportedLibraries) { | 278 in exportedLibrary.exportedLibraries) { |
| 273 addTransitiveExportClosure(transitiveExport); | 279 addTransitiveExportClosure(transitiveExport); |
| 274 } | 280 } |
| 275 } | 281 } |
| 276 } | 282 } |
| 277 | 283 |
| 278 /** | 284 /** |
| 285 * Fill in [prefixMap] using information from [libraryElement.imports]. | |
| 286 */ | |
| 287 void computePrefixMap() { | |
| 288 for (ImportElement import in libraryElement.imports) { | |
|
scheglov
2016/01/09 22:51:31
We could skip analyzing an import if it does not h
Paul Berry
2016/01/10 02:08:28
Done.
| |
| 289 import.importedLibrary.exportNamespace.definedNames | |
| 290 .forEach((String name, Element e) { | |
| 291 if (import.combinators.any((NamespaceCombinator combinator) => | |
| 292 doesCombinatorReject(combinator, name))) { | |
| 293 return; | |
| 294 } | |
| 295 prefixMap[e] = import.prefix; | |
| 296 }); | |
| 297 } | |
| 298 } | |
| 299 | |
| 300 /** | |
| 301 * Determine if the given [combinator] would reject an element having the | |
| 302 * given [name]. | |
| 303 */ | |
| 304 bool doesCombinatorReject(NamespaceCombinator combinator, String name) { | |
| 305 if (combinator is ShowElementCombinator) { | |
| 306 return !combinator.shownNames.contains(name); | |
| 307 } else if (combinator is HideElementCombinator) { | |
| 308 return combinator.hiddenNames.contains(name); | |
| 309 } else { | |
| 310 throw new StateError( | |
| 311 'Unexpected combinator type ${combinator.runtimeType}'); | |
| 312 } | |
| 313 } | |
| 314 | |
| 315 /** | |
| 279 * Compute the appropriate De Bruijn index to represent the given type | 316 * Compute the appropriate De Bruijn index to represent the given type |
| 280 * parameter [type]. | 317 * parameter [type]. |
| 281 */ | 318 */ |
| 282 int findTypeParameterIndex(TypeParameterType type, Element context) { | 319 int findTypeParameterIndex(TypeParameterType type, Element context) { |
| 283 int index = 0; | 320 int index = 0; |
| 284 while (context != null) { | 321 while (context != null) { |
| 285 List<TypeParameterElement> typeParameters; | 322 List<TypeParameterElement> typeParameters; |
| 286 if (context is ClassElement) { | 323 if (context is ClassElement) { |
| 287 typeParameters = context.typeParameters; | 324 typeParameters = context.typeParameters; |
| 288 } else if (context is FunctionTypeAliasElement) { | 325 } else if (context is FunctionTypeAliasElement) { |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 512 } | 549 } |
| 513 | 550 |
| 514 /** | 551 /** |
| 515 * Serialize the whole library element into a [PrelinkedLibrary]. Should be | 552 * Serialize the whole library element into a [PrelinkedLibrary]. Should be |
| 516 * called exactly once for each instance of [_LibrarySerializer]. | 553 * called exactly once for each instance of [_LibrarySerializer]. |
| 517 * | 554 * |
| 518 * The unlinked compilation units are stored in [unlinkedUnits], and their | 555 * The unlinked compilation units are stored in [unlinkedUnits], and their |
| 519 * absolute URIs are stored in [unitUris]. | 556 * absolute URIs are stored in [unitUris]. |
| 520 */ | 557 */ |
| 521 PrelinkedLibraryBuilder serializeLibrary() { | 558 PrelinkedLibraryBuilder serializeLibrary() { |
| 559 computePrefixMap(); | |
| 522 PrelinkedLibraryBuilder pb = new PrelinkedLibraryBuilder(ctx); | 560 PrelinkedLibraryBuilder pb = new PrelinkedLibraryBuilder(ctx); |
| 523 addCompilationUnitElements(libraryElement.definingCompilationUnit, 0); | 561 addCompilationUnitElements(libraryElement.definingCompilationUnit, 0); |
| 524 for (int i = 0; i < libraryElement.parts.length; i++) { | 562 for (int i = 0; i < libraryElement.parts.length; i++) { |
| 525 addCompilationUnitElements(libraryElement.parts[i], i + 1); | 563 addCompilationUnitElements(libraryElement.parts[i], i + 1); |
| 526 } | 564 } |
| 527 pb.units = prelinkedUnits; | 565 pb.units = prelinkedUnits; |
| 528 pb.dependencies = dependencies; | 566 pb.dependencies = dependencies; |
| 529 pb.importDependencies = prelinkedImports; | 567 pb.importDependencies = prelinkedImports; |
| 530 return pb; | 568 return pb; |
| 531 } | 569 } |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 636 assert(unlinkedReferences.length == prelinkedReferences.length); | 674 assert(unlinkedReferences.length == prelinkedReferences.length); |
| 637 CompilationUnitElement unitElement = | 675 CompilationUnitElement unitElement = |
| 638 element.getAncestor((Element e) => e is CompilationUnitElement); | 676 element.getAncestor((Element e) => e is CompilationUnitElement); |
| 639 int unit = dependentLibrary.units.indexOf(unitElement); | 677 int unit = dependentLibrary.units.indexOf(unitElement); |
| 640 assert(unit != -1); | 678 assert(unit != -1); |
| 641 int numTypeParameters = 0; | 679 int numTypeParameters = 0; |
| 642 if (element is TypeParameterizedElement) { | 680 if (element is TypeParameterizedElement) { |
| 643 numTypeParameters = element.typeParameters.length; | 681 numTypeParameters = element.typeParameters.length; |
| 644 } | 682 } |
| 645 int index = unlinkedReferences.length; | 683 int index = unlinkedReferences.length; |
| 646 // TODO(paulberry): set UnlinkedReference.prefix. | 684 // Figure out a prefix that may be used to refer to the given type. |
| 647 unlinkedReferences | 685 // TODO(paulberry): to avoid subtle relinking inconsistencies we |
| 648 .add(encodeUnlinkedReference(ctx, name: element.name)); | 686 // should use the actual prefix from the AST (a given type may be |
| 687 // reachable via multiple prefixes), but sadly, this information is | |
| 688 // not recorded in the element model. | |
| 689 int prefixReference = 0; | |
| 690 PrefixElement prefix = prefixMap[element]; | |
| 691 if (prefix != null) { | |
| 692 prefixReference = serializePrefix(prefix); | |
| 693 } | |
| 694 unlinkedReferences.add(encodeUnlinkedReference(ctx, | |
| 695 name: element.name, prefixReference: prefixReference)); | |
| 649 prelinkedReferences.add(encodePrelinkedReference(ctx, | 696 prelinkedReferences.add(encodePrelinkedReference(ctx, |
| 650 dependency: serializeDependency(dependentLibrary), | 697 dependency: serializeDependency(dependentLibrary), |
| 651 kind: element is FunctionTypeAliasElement | 698 kind: element is FunctionTypeAliasElement |
| 652 ? PrelinkedReferenceKind.typedef | 699 ? PrelinkedReferenceKind.typedef |
| 653 : PrelinkedReferenceKind.classOrEnum, | 700 : PrelinkedReferenceKind.classOrEnum, |
| 654 unit: unit, | 701 unit: unit, |
| 655 numTypeParameters: numTypeParameters)); | 702 numTypeParameters: numTypeParameters)); |
| 656 return index; | 703 return index; |
| 657 }); | 704 }); |
| 658 } | 705 } |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 701 b.nameOffset = variable.nameOffset; | 748 b.nameOffset = variable.nameOffset; |
| 702 b.type = serializeTypeRef(variable.type, variable); | 749 b.type = serializeTypeRef(variable.type, variable); |
| 703 b.isStatic = variable.isStatic && variable.enclosingElement is ClassElement; | 750 b.isStatic = variable.isStatic && variable.enclosingElement is ClassElement; |
| 704 b.isFinal = variable.isFinal; | 751 b.isFinal = variable.isFinal; |
| 705 b.isConst = variable.isConst; | 752 b.isConst = variable.isConst; |
| 706 b.hasImplicitType = variable.hasImplicitType; | 753 b.hasImplicitType = variable.hasImplicitType; |
| 707 b.documentationComment = serializeDocumentation(variable); | 754 b.documentationComment = serializeDocumentation(variable); |
| 708 return b; | 755 return b; |
| 709 } | 756 } |
| 710 } | 757 } |
| OLD | NEW |