| 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'; |
| 11 import 'package:analyzer/src/generated/utilities_dart.dart'; | 11 import 'package:analyzer/src/generated/utilities_dart.dart'; |
| 12 import 'package:analyzer/src/summary/format.dart'; | 12 import 'package:analyzer/src/summary/format.dart'; |
| 13 import 'package:analyzer/src/summary/name_filter.dart'; | 13 import 'package:analyzer/src/summary/name_filter.dart'; |
| 14 | 14 |
| 15 /** | 15 /** |
| 16 * Serialize all the elements in [lib] to a summary using [ctx] as the context | 16 * Serialize all the elements in [lib] to a summary using [ctx] as the context |
| 17 * for building the summary, and using [typeProvider] to find built-in types. | 17 * for building the summary, and using [typeProvider] to find built-in types. |
| 18 */ | 18 */ |
| 19 LibrarySerializationResult serializeLibrary( | 19 LibrarySerializationResult serializeLibrary( |
| 20 LibraryElement lib, TypeProvider typeProvider) { | 20 LibraryElement lib, TypeProvider typeProvider) { |
| 21 var serializer = new _LibrarySerializer(lib, typeProvider); | 21 var serializer = new _LibrarySerializer(lib, typeProvider); |
| 22 PrelinkedLibraryBuilder prelinked = serializer.serializeLibrary(); | 22 LinkedLibraryBuilder linked = serializer.serializeLibrary(); |
| 23 return new LibrarySerializationResult( | 23 return new LibrarySerializationResult( |
| 24 prelinked, serializer.unlinkedUnits, serializer.unitUris); | 24 linked, serializer.unlinkedUnits, serializer.unitUris); |
| 25 } | 25 } |
| 26 | 26 |
| 27 /** | 27 /** |
| 28 * Data structure holding the result of serializing a [LibraryElement]. | 28 * Data structure holding the result of serializing a [LibraryElement]. |
| 29 */ | 29 */ |
| 30 class LibrarySerializationResult { | 30 class LibrarySerializationResult { |
| 31 /** | 31 /** |
| 32 * Pre-linked information the given library. | 32 * Linked information the given library. |
| 33 */ | 33 */ |
| 34 final PrelinkedLibraryBuilder prelinked; | 34 final LinkedLibraryBuilder linked; |
| 35 | 35 |
| 36 /** | 36 /** |
| 37 * Unlinked information for the compilation units constituting the library. | 37 * Unlinked information for the compilation units constituting the library. |
| 38 * The zeroth entry in the list is the defining compilation unit; the | 38 * The zeroth entry in the list is the defining compilation unit; the |
| 39 * remaining entries are the parts, in the order listed in the defining | 39 * remaining entries are the parts, in the order listed in the defining |
| 40 * compilation unit's part declarations. | 40 * compilation unit's part declarations. |
| 41 */ | 41 */ |
| 42 final List<UnlinkedUnitBuilder> unlinkedUnits; | 42 final List<UnlinkedUnitBuilder> unlinkedUnits; |
| 43 | 43 |
| 44 /** | 44 /** |
| 45 * Absolute URI of each compilation unit appearing in the library. | 45 * Absolute URI of each compilation unit appearing in the library. |
| 46 */ | 46 */ |
| 47 final List<String> unitUris; | 47 final List<String> unitUris; |
| 48 | 48 |
| 49 LibrarySerializationResult(this.prelinked, this.unlinkedUnits, this.unitUris); | 49 LibrarySerializationResult(this.linked, this.unlinkedUnits, this.unitUris); |
| 50 } | 50 } |
| 51 | 51 |
| 52 /** | 52 /** |
| 53 * Instances of this class keep track of intermediate state during | 53 * Instances of this class keep track of intermediate state during |
| 54 * serialization of a single library.` | 54 * serialization of a single library.` |
| 55 */ | 55 */ |
| 56 class _LibrarySerializer { | 56 class _LibrarySerializer { |
| 57 /** | 57 /** |
| 58 * The library to be serialized. | 58 * The library to be serialized. |
| 59 */ | 59 */ |
| 60 final LibraryElement libraryElement; | 60 final LibraryElement libraryElement; |
| 61 | 61 |
| 62 /** | 62 /** |
| 63 * The type provider. This is used to locate the library for `dart:core`. | 63 * The type provider. This is used to locate the library for `dart:core`. |
| 64 */ | 64 */ |
| 65 final TypeProvider typeProvider; | 65 final TypeProvider typeProvider; |
| 66 | 66 |
| 67 /** | 67 /** |
| 68 * List of objects which should be written to [PrelinkedLibrary.units]. | 68 * List of objects which should be written to [LinkedLibrary.units]. |
| 69 */ | 69 */ |
| 70 final List<PrelinkedUnitBuilder> prelinkedUnits = <PrelinkedUnitBuilder>[]; | 70 final List<LinkedUnitBuilder> linkedUnits = <LinkedUnitBuilder>[]; |
| 71 | 71 |
| 72 /** | 72 /** |
| 73 * List of unlinked units corresponding to the pre-linked units in | 73 * List of unlinked units corresponding to the linked units in |
| 74 * [prelinkedUnits], | 74 * [linkedUnits], |
| 75 */ | 75 */ |
| 76 final List<UnlinkedUnitBuilder> unlinkedUnits = <UnlinkedUnitBuilder>[]; | 76 final List<UnlinkedUnitBuilder> unlinkedUnits = <UnlinkedUnitBuilder>[]; |
| 77 | 77 |
| 78 /** | 78 /** |
| 79 * List of absolute URIs of the compilation units in the library. | 79 * List of absolute URIs of the compilation units in the library. |
| 80 */ | 80 */ |
| 81 final List<String> unitUris = <String>[]; | 81 final List<String> unitUris = <String>[]; |
| 82 | 82 |
| 83 /** | 83 /** |
| 84 * Map from [LibraryElement] to the index of the entry in the "dependency | 84 * Map from [LibraryElement] to the index of the entry in the "dependency |
| 85 * table" that refers to it. | 85 * table" that refers to it. |
| 86 */ | 86 */ |
| 87 final Map<LibraryElement, int> dependencyMap = <LibraryElement, int>{}; | 87 final Map<LibraryElement, int> dependencyMap = <LibraryElement, int>{}; |
| 88 | 88 |
| 89 /** | 89 /** |
| 90 * The "dependency table". This is the list of objects which should be | 90 * The "dependency table". This is the list of objects which should be |
| 91 * written to [PrelinkedLibrary.dependencies]. | 91 * written to [LinkedLibrary.dependencies]. |
| 92 */ | 92 */ |
| 93 final List<PrelinkedDependencyBuilder> dependencies = | 93 final List<LinkedDependencyBuilder> dependencies = |
| 94 <PrelinkedDependencyBuilder>[]; | 94 <LinkedDependencyBuilder>[]; |
| 95 | 95 |
| 96 /** | 96 /** |
| 97 * The prelinked portion of the "imports table". This is the list of ints | 97 * The linked portion of the "imports table". This is the list of ints |
| 98 * which should be written to [PrelinkedLibrary.imports]. | 98 * which should be written to [LinkedLibrary.imports]. |
| 99 */ | 99 */ |
| 100 final List<int> prelinkedImports = <int>[]; | 100 final List<int> linkedImports = <int>[]; |
| 101 | 101 |
| 102 /** | 102 /** |
| 103 * Map from [Element] to the index of the entry in the "references table" | 103 * Map from [Element] to the index of the entry in the "references table" |
| 104 * that refers to it. | 104 * that refers to it. |
| 105 */ | 105 */ |
| 106 final Map<Element, int> referenceMap = <Element, int>{}; | 106 final Map<Element, int> referenceMap = <Element, int>{}; |
| 107 | 107 |
| 108 /** | 108 /** |
| 109 * The unlinked portion of the "references table". This is the list of | 109 * The unlinked portion of the "references table". This is the list of |
| 110 * objects which should be written to [UnlinkedUnit.references]. | 110 * objects which should be written to [UnlinkedUnit.references]. |
| 111 */ | 111 */ |
| 112 List<UnlinkedReferenceBuilder> unlinkedReferences; | 112 List<UnlinkedReferenceBuilder> unlinkedReferences; |
| 113 | 113 |
| 114 /** | 114 /** |
| 115 * The prelinked portion of the "references table". This is the list of | 115 * The linked portion of the "references table". This is the list of |
| 116 * objects which should be written to [PrelinkedUnit.references]. | 116 * objects which should be written to [LinkedUnit.references]. |
| 117 */ | 117 */ |
| 118 List<PrelinkedReferenceBuilder> prelinkedReferences; | 118 List<LinkedReferenceBuilder> linkedReferences; |
| 119 | 119 |
| 120 //final Map<String, int> prefixIndices = <String, int>{}; | 120 //final Map<String, int> prefixIndices = <String, int>{}; |
| 121 | 121 |
| 122 /** | 122 /** |
| 123 * Index into the "references table" representing an unresolved reference, if | 123 * Index into the "references table" representing an unresolved reference, if |
| 124 * such an index exists. `null` if no such entry has been made in the | 124 * such an index exists. `null` if no such entry has been made in the |
| 125 * references table yet. | 125 * references table yet. |
| 126 */ | 126 */ |
| 127 int unresolvedReferenceIndex = null; | 127 int unresolvedReferenceIndex = null; |
| 128 | 128 |
| 129 /** | 129 /** |
| 130 * Set of libraries which have been seen so far while visiting the transitive | 130 * Set of libraries which have been seen so far while visiting the transitive |
| 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 * Map from imported element to the prefix which may be used to refer to that | 137 * Map from imported element to the prefix which may be used to refer to that |
| 138 * element; elements for which no prefix is needed are absent from this map. | 138 * element; elements for which no prefix is needed are absent from this map. |
| 139 */ | 139 */ |
| 140 final Map<Element, PrefixElement> prefixMap = <Element, PrefixElement>{}; | 140 final Map<Element, PrefixElement> prefixMap = <Element, PrefixElement>{}; |
| 141 | 141 |
| 142 _LibrarySerializer(this.libraryElement, this.typeProvider) { | 142 _LibrarySerializer(this.libraryElement, this.typeProvider) { |
| 143 dependencies.add(new PrelinkedDependencyBuilder()); | 143 dependencies.add(new LinkedDependencyBuilder()); |
| 144 dependencyMap[libraryElement] = 0; | 144 dependencyMap[libraryElement] = 0; |
| 145 } | 145 } |
| 146 | 146 |
| 147 /** | 147 /** |
| 148 * Retrieve the library element for `dart:core`. | 148 * Retrieve the library element for `dart:core`. |
| 149 */ | 149 */ |
| 150 LibraryElement get coreLibrary => typeProvider.objectType.element.library; | 150 LibraryElement get coreLibrary => typeProvider.objectType.element.library; |
| 151 | 151 |
| 152 /** | 152 /** |
| 153 * Add all classes, enums, typedefs, executables, and top level variables | 153 * Add all classes, enums, typedefs, executables, and top level variables |
| 154 * from the given compilation unit [element] to the library summary. | 154 * from the given compilation unit [element] to the library summary. |
| 155 * [unitNum] indicates the ordinal position of this compilation unit in the | 155 * [unitNum] indicates the ordinal position of this compilation unit in the |
| 156 * library. | 156 * library. |
| 157 */ | 157 */ |
| 158 void addCompilationUnitElements(CompilationUnitElement element, int unitNum) { | 158 void addCompilationUnitElements(CompilationUnitElement element, int unitNum) { |
| 159 UnlinkedUnitBuilder b = new UnlinkedUnitBuilder(); | 159 UnlinkedUnitBuilder b = new UnlinkedUnitBuilder(); |
| 160 referenceMap.clear(); | 160 referenceMap.clear(); |
| 161 unlinkedReferences = <UnlinkedReferenceBuilder>[ | 161 unlinkedReferences = <UnlinkedReferenceBuilder>[ |
| 162 new UnlinkedReferenceBuilder() | 162 new UnlinkedReferenceBuilder() |
| 163 ]; | 163 ]; |
| 164 prelinkedReferences = <PrelinkedReferenceBuilder>[ | 164 linkedReferences = <LinkedReferenceBuilder>[ |
| 165 new PrelinkedReferenceBuilder(kind: PrelinkedReferenceKind.classOrEnum) | 165 new LinkedReferenceBuilder(kind: ReferenceKind.classOrEnum) |
| 166 ]; | 166 ]; |
| 167 List<UnlinkedPublicNameBuilder> names = <UnlinkedPublicNameBuilder>[]; | 167 List<UnlinkedPublicNameBuilder> names = <UnlinkedPublicNameBuilder>[]; |
| 168 for (PropertyAccessorElement accessor in element.accessors) { | 168 for (PropertyAccessorElement accessor in element.accessors) { |
| 169 if (accessor.isPublic) { | 169 if (accessor.isPublic) { |
| 170 names.add(new UnlinkedPublicNameBuilder( | 170 names.add(new UnlinkedPublicNameBuilder( |
| 171 kind: PrelinkedReferenceKind.topLevelPropertyAccessor, | 171 kind: ReferenceKind.topLevelPropertyAccessor, |
| 172 name: accessor.name, | 172 name: accessor.name, |
| 173 numTypeParameters: accessor.typeParameters.length)); | 173 numTypeParameters: accessor.typeParameters.length)); |
| 174 } | 174 } |
| 175 } | 175 } |
| 176 for (ClassElement cls in element.types) { | 176 for (ClassElement cls in element.types) { |
| 177 if (cls.isPublic) { | 177 if (cls.isPublic) { |
| 178 names.add(new UnlinkedPublicNameBuilder( | 178 names.add(new UnlinkedPublicNameBuilder( |
| 179 kind: PrelinkedReferenceKind.classOrEnum, | 179 kind: ReferenceKind.classOrEnum, |
| 180 name: cls.name, | 180 name: cls.name, |
| 181 numTypeParameters: cls.typeParameters.length)); | 181 numTypeParameters: cls.typeParameters.length)); |
| 182 } | 182 } |
| 183 } | 183 } |
| 184 for (ClassElement enm in element.enums) { | 184 for (ClassElement enm in element.enums) { |
| 185 if (enm.isPublic) { | 185 if (enm.isPublic) { |
| 186 names.add(new UnlinkedPublicNameBuilder( | 186 names.add(new UnlinkedPublicNameBuilder( |
| 187 kind: PrelinkedReferenceKind.classOrEnum, name: enm.name)); | 187 kind: ReferenceKind.classOrEnum, name: enm.name)); |
| 188 } | 188 } |
| 189 } | 189 } |
| 190 for (FunctionElement function in element.functions) { | 190 for (FunctionElement function in element.functions) { |
| 191 if (function.isPublic) { | 191 if (function.isPublic) { |
| 192 names.add(new UnlinkedPublicNameBuilder( | 192 names.add(new UnlinkedPublicNameBuilder( |
| 193 kind: PrelinkedReferenceKind.topLevelFunction, | 193 kind: ReferenceKind.topLevelFunction, |
| 194 name: function.name, | 194 name: function.name, |
| 195 numTypeParameters: function.typeParameters.length)); | 195 numTypeParameters: function.typeParameters.length)); |
| 196 } | 196 } |
| 197 } | 197 } |
| 198 for (FunctionTypeAliasElement typedef in element.functionTypeAliases) { | 198 for (FunctionTypeAliasElement typedef in element.functionTypeAliases) { |
| 199 if (typedef.isPublic) { | 199 if (typedef.isPublic) { |
| 200 names.add(new UnlinkedPublicNameBuilder( | 200 names.add(new UnlinkedPublicNameBuilder( |
| 201 kind: PrelinkedReferenceKind.typedef, | 201 kind: ReferenceKind.typedef, |
| 202 name: typedef.name, | 202 name: typedef.name, |
| 203 numTypeParameters: typedef.typeParameters.length)); | 203 numTypeParameters: typedef.typeParameters.length)); |
| 204 } | 204 } |
| 205 } | 205 } |
| 206 if (unitNum == 0) { | 206 if (unitNum == 0) { |
| 207 if (libraryElement.name.isNotEmpty) { | 207 if (libraryElement.name.isNotEmpty) { |
| 208 b.libraryName = libraryElement.name; | 208 b.libraryName = libraryElement.name; |
| 209 b.libraryNameOffset = libraryElement.nameOffset; | 209 b.libraryNameOffset = libraryElement.nameOffset; |
| 210 b.libraryNameLength = libraryElement.nameLength; | 210 b.libraryNameLength = libraryElement.nameLength; |
| 211 b.libraryDocumentationComment = serializeDocumentation(libraryElement); | 211 b.libraryDocumentationComment = serializeDocumentation(libraryElement); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 PropertyInducingElement variable = accessor.variable; | 248 PropertyInducingElement variable = accessor.variable; |
| 249 if (variable != null) { | 249 if (variable != null) { |
| 250 assert(!variable.isSynthetic); | 250 assert(!variable.isSynthetic); |
| 251 variables.add(serializeVariable(variable)); | 251 variables.add(serializeVariable(variable)); |
| 252 } | 252 } |
| 253 } | 253 } |
| 254 } | 254 } |
| 255 b.variables = variables; | 255 b.variables = variables; |
| 256 b.references = unlinkedReferences; | 256 b.references = unlinkedReferences; |
| 257 unlinkedUnits.add(b); | 257 unlinkedUnits.add(b); |
| 258 prelinkedUnits | 258 linkedUnits.add(new LinkedUnitBuilder(references: linkedReferences)); |
| 259 .add(new PrelinkedUnitBuilder(references: prelinkedReferences)); | |
| 260 unitUris.add(element.source.uri.toString()); | 259 unitUris.add(element.source.uri.toString()); |
| 261 unlinkedReferences = null; | 260 unlinkedReferences = null; |
| 262 prelinkedReferences = null; | 261 linkedReferences = null; |
| 263 } | 262 } |
| 264 | 263 |
| 265 /** | 264 /** |
| 266 * Add [exportedLibrary] (and the transitive closure of all libraries it | 265 * Add [exportedLibrary] (and the transitive closure of all libraries it |
| 267 * exports) to the dependency table ([PrelinkedLibrary.dependencies]). | 266 * exports) to the dependency table ([LinkedLibrary.dependencies]). |
| 268 */ | 267 */ |
| 269 void addTransitiveExportClosure(LibraryElement exportedLibrary) { | 268 void addTransitiveExportClosure(LibraryElement exportedLibrary) { |
| 270 if (librariesAddedToTransitiveExportClosure.add(exportedLibrary)) { | 269 if (librariesAddedToTransitiveExportClosure.add(exportedLibrary)) { |
| 271 serializeDependency(exportedLibrary); | 270 serializeDependency(exportedLibrary); |
| 272 for (LibraryElement transitiveExport | 271 for (LibraryElement transitiveExport |
| 273 in exportedLibrary.exportedLibraries) { | 272 in exportedLibrary.exportedLibraries) { |
| 274 addTransitiveExportClosure(transitiveExport); | 273 addTransitiveExportClosure(transitiveExport); |
| 275 } | 274 } |
| 276 } | 275 } |
| 277 } | 276 } |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 if (combinator is ShowElementCombinator) { | 379 if (combinator is ShowElementCombinator) { |
| 381 b.shows = combinator.shownNames; | 380 b.shows = combinator.shownNames; |
| 382 } else if (combinator is HideElementCombinator) { | 381 } else if (combinator is HideElementCombinator) { |
| 383 b.hides = combinator.hiddenNames; | 382 b.hides = combinator.hiddenNames; |
| 384 } | 383 } |
| 385 return b; | 384 return b; |
| 386 } | 385 } |
| 387 | 386 |
| 388 /** | 387 /** |
| 389 * Return the index of the entry in the dependency table | 388 * Return the index of the entry in the dependency table |
| 390 * ([PrelinkedLibrary.dependencies]) for the given [dependentLibrary]. A new | 389 * ([LinkedLibrary.dependencies]) for the given [dependentLibrary]. A new |
| 391 * entry is added to the table if necessary to satisfy the request. | 390 * entry is added to the table if necessary to satisfy the request. |
| 392 */ | 391 */ |
| 393 int serializeDependency(LibraryElement dependentLibrary) { | 392 int serializeDependency(LibraryElement dependentLibrary) { |
| 394 return dependencyMap.putIfAbsent(dependentLibrary, () { | 393 return dependencyMap.putIfAbsent(dependentLibrary, () { |
| 395 int index = dependencies.length; | 394 int index = dependencies.length; |
| 396 List<String> parts = dependentLibrary.parts | 395 List<String> parts = dependentLibrary.parts |
| 397 .map((CompilationUnitElement e) => e.source.uri.toString()) | 396 .map((CompilationUnitElement e) => e.source.uri.toString()) |
| 398 .toList(); | 397 .toList(); |
| 399 dependencies.add(new PrelinkedDependencyBuilder( | 398 dependencies.add(new LinkedDependencyBuilder( |
| 400 uri: dependentLibrary.source.uri.toString(), parts: parts)); | 399 uri: dependentLibrary.source.uri.toString(), parts: parts)); |
| 401 return index; | 400 return index; |
| 402 }); | 401 }); |
| 403 } | 402 } |
| 404 | 403 |
| 405 /** | 404 /** |
| 406 * Serialize documentation from the given [element], creating an | 405 * Serialize documentation from the given [element], creating an |
| 407 * [UnlinkedDocumentationComment]. | 406 * [UnlinkedDocumentationComment]. |
| 408 * | 407 * |
| 409 * If [element] has no documentation, `null` is returned. | 408 * If [element] has no documentation, `null` is returned. |
| 410 */ | 409 */ |
| 411 UnlinkedDocumentationCommentBuilder serializeDocumentation(Element element) { | 410 UnlinkedDocumentationCommentBuilder serializeDocumentation(Element element) { |
| 412 if (element.documentationComment == null) { | 411 if (element.documentationComment == null) { |
| 413 return null; | 412 return null; |
| 414 } | 413 } |
| 415 return new UnlinkedDocumentationCommentBuilder( | 414 return new UnlinkedDocumentationCommentBuilder( |
| 416 text: element.documentationComment, | 415 text: element.documentationComment, |
| 417 offset: element.docRange.offset, | 416 offset: element.docRange.offset, |
| 418 length: element.docRange.length); | 417 length: element.docRange.length); |
| 419 } | 418 } |
| 420 | 419 |
| 421 /** | 420 /** |
| 422 * Return the index of the entry in the references table | 421 * Return the index of the entry in the references table |
| 423 * ([UnlinkedLibrary.references] and [PrelinkedLibrary.references]) | 422 * ([UnlinkedLibrary.references] and [LinkedLibrary.references]) |
| 424 * representing the pseudo-type `dynamic`. | 423 * representing the pseudo-type `dynamic`. |
| 425 */ | 424 */ |
| 426 int serializeDynamicReference() => 0; | 425 int serializeDynamicReference() => 0; |
| 427 | 426 |
| 428 /** | 427 /** |
| 429 * Serialize the given [enumElement], creating an [UnlinkedEnum]. | 428 * Serialize the given [enumElement], creating an [UnlinkedEnum]. |
| 430 */ | 429 */ |
| 431 UnlinkedEnumBuilder serializeEnum(ClassElement enumElement) { | 430 UnlinkedEnumBuilder serializeEnum(ClassElement enumElement) { |
| 432 UnlinkedEnumBuilder b = new UnlinkedEnumBuilder(); | 431 UnlinkedEnumBuilder b = new UnlinkedEnumBuilder(); |
| 433 b.name = enumElement.name; | 432 b.name = enumElement.name; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 ExportElement exportElement) { | 503 ExportElement exportElement) { |
| 505 addTransitiveExportClosure(exportElement.exportedLibrary); | 504 addTransitiveExportClosure(exportElement.exportedLibrary); |
| 506 UnlinkedExportPublicBuilder b = new UnlinkedExportPublicBuilder(); | 505 UnlinkedExportPublicBuilder b = new UnlinkedExportPublicBuilder(); |
| 507 b.uri = exportElement.uri; | 506 b.uri = exportElement.uri; |
| 508 b.combinators = exportElement.combinators.map(serializeCombinator).toList(); | 507 b.combinators = exportElement.combinators.map(serializeCombinator).toList(); |
| 509 return b; | 508 return b; |
| 510 } | 509 } |
| 511 | 510 |
| 512 /** | 511 /** |
| 513 * Serialize the given [importElement] yielding an [UnlinkedImportBuilder]. | 512 * Serialize the given [importElement] yielding an [UnlinkedImportBuilder]. |
| 514 * Also, add pre-linked information about it to the [prelinkedImports] list. | 513 * Also, add linked information about it to the [linkedImports] list. |
| 515 */ | 514 */ |
| 516 UnlinkedImportBuilder serializeImport(ImportElement importElement) { | 515 UnlinkedImportBuilder serializeImport(ImportElement importElement) { |
| 517 UnlinkedImportBuilder b = new UnlinkedImportBuilder(); | 516 UnlinkedImportBuilder b = new UnlinkedImportBuilder(); |
| 518 b.isDeferred = importElement.isDeferred; | 517 b.isDeferred = importElement.isDeferred; |
| 519 b.offset = importElement.nameOffset; | 518 b.offset = importElement.nameOffset; |
| 520 b.combinators = importElement.combinators.map(serializeCombinator).toList(); | 519 b.combinators = importElement.combinators.map(serializeCombinator).toList(); |
| 521 if (importElement.prefix != null) { | 520 if (importElement.prefix != null) { |
| 522 b.prefixReference = serializePrefix(importElement.prefix); | 521 b.prefixReference = serializePrefix(importElement.prefix); |
| 523 b.prefixOffset = importElement.prefix.nameOffset; | 522 b.prefixOffset = importElement.prefix.nameOffset; |
| 524 } | 523 } |
| 525 if (importElement.isSynthetic) { | 524 if (importElement.isSynthetic) { |
| 526 b.isImplicit = true; | 525 b.isImplicit = true; |
| 527 } else { | 526 } else { |
| 528 b.uri = importElement.uri; | 527 b.uri = importElement.uri; |
| 529 b.uriOffset = importElement.uriOffset; | 528 b.uriOffset = importElement.uriOffset; |
| 530 b.uriEnd = importElement.uriEnd; | 529 b.uriEnd = importElement.uriEnd; |
| 531 } | 530 } |
| 532 addTransitiveExportClosure(importElement.importedLibrary); | 531 addTransitiveExportClosure(importElement.importedLibrary); |
| 533 prelinkedImports.add(serializeDependency(importElement.importedLibrary)); | 532 linkedImports.add(serializeDependency(importElement.importedLibrary)); |
| 534 return b; | 533 return b; |
| 535 } | 534 } |
| 536 | 535 |
| 537 /** | 536 /** |
| 538 * Serialize the whole library element into a [PrelinkedLibrary]. Should be | 537 * Serialize the whole library element into a [LinkedLibrary]. Should be |
| 539 * called exactly once for each instance of [_LibrarySerializer]. | 538 * called exactly once for each instance of [_LibrarySerializer]. |
| 540 * | 539 * |
| 541 * The unlinked compilation units are stored in [unlinkedUnits], and their | 540 * The unlinked compilation units are stored in [unlinkedUnits], and their |
| 542 * absolute URIs are stored in [unitUris]. | 541 * absolute URIs are stored in [unitUris]. |
| 543 */ | 542 */ |
| 544 PrelinkedLibraryBuilder serializeLibrary() { | 543 LinkedLibraryBuilder serializeLibrary() { |
| 545 computePrefixMap(); | 544 computePrefixMap(); |
| 546 PrelinkedLibraryBuilder pb = new PrelinkedLibraryBuilder(); | 545 LinkedLibraryBuilder pb = new LinkedLibraryBuilder(); |
| 547 addCompilationUnitElements(libraryElement.definingCompilationUnit, 0); | 546 addCompilationUnitElements(libraryElement.definingCompilationUnit, 0); |
| 548 for (int i = 0; i < libraryElement.parts.length; i++) { | 547 for (int i = 0; i < libraryElement.parts.length; i++) { |
| 549 addCompilationUnitElements(libraryElement.parts[i], i + 1); | 548 addCompilationUnitElements(libraryElement.parts[i], i + 1); |
| 550 } | 549 } |
| 551 pb.units = prelinkedUnits; | 550 pb.units = linkedUnits; |
| 552 pb.dependencies = dependencies; | 551 pb.dependencies = dependencies; |
| 553 pb.importDependencies = prelinkedImports; | 552 pb.importDependencies = linkedImports; |
| 554 List<String> exportedNames = | 553 List<String> exportedNames = |
| 555 libraryElement.exportNamespace.definedNames.keys.toList(); | 554 libraryElement.exportNamespace.definedNames.keys.toList(); |
| 556 exportedNames.sort(); | 555 exportedNames.sort(); |
| 557 List<PrelinkedExportNameBuilder> exportNames = | 556 List<LinkedExportNameBuilder> exportNames = <LinkedExportNameBuilder>[]; |
| 558 <PrelinkedExportNameBuilder>[]; | |
| 559 for (String name in exportedNames) { | 557 for (String name in exportedNames) { |
| 560 if (libraryElement.publicNamespace.definedNames.containsKey(name)) { | 558 if (libraryElement.publicNamespace.definedNames.containsKey(name)) { |
| 561 continue; | 559 continue; |
| 562 } | 560 } |
| 563 Element element = libraryElement.exportNamespace.get(name); | 561 Element element = libraryElement.exportNamespace.get(name); |
| 564 LibraryElement dependentLibrary = element.library; | 562 LibraryElement dependentLibrary = element.library; |
| 565 CompilationUnitElement unitElement = | 563 CompilationUnitElement unitElement = |
| 566 element.getAncestor((Element e) => e is CompilationUnitElement); | 564 element.getAncestor((Element e) => e is CompilationUnitElement); |
| 567 int unit = dependentLibrary.units.indexOf(unitElement); | 565 int unit = dependentLibrary.units.indexOf(unitElement); |
| 568 assert(unit != -1); | 566 assert(unit != -1); |
| 569 PrelinkedReferenceKind kind; | 567 ReferenceKind kind; |
| 570 if (element is PropertyAccessorElement) { | 568 if (element is PropertyAccessorElement) { |
| 571 kind = PrelinkedReferenceKind.topLevelPropertyAccessor; | 569 kind = ReferenceKind.topLevelPropertyAccessor; |
| 572 } else if (element is FunctionTypeAliasElement) { | 570 } else if (element is FunctionTypeAliasElement) { |
| 573 kind = PrelinkedReferenceKind.typedef; | 571 kind = ReferenceKind.typedef; |
| 574 } else if (element is ClassElement) { | 572 } else if (element is ClassElement) { |
| 575 kind = PrelinkedReferenceKind.classOrEnum; | 573 kind = ReferenceKind.classOrEnum; |
| 576 } else if (element is FunctionElement) { | 574 } else if (element is FunctionElement) { |
| 577 kind = PrelinkedReferenceKind.topLevelFunction; | 575 kind = ReferenceKind.topLevelFunction; |
| 578 } else { | 576 } else { |
| 579 throw new Exception('Unexpected element kind: ${element.runtimeType}'); | 577 throw new Exception('Unexpected element kind: ${element.runtimeType}'); |
| 580 } | 578 } |
| 581 exportNames.add(new PrelinkedExportNameBuilder( | 579 exportNames.add(new LinkedExportNameBuilder( |
| 582 name: name, | 580 name: name, |
| 583 dependency: serializeDependency(dependentLibrary), | 581 dependency: serializeDependency(dependentLibrary), |
| 584 unit: unit, | 582 unit: unit, |
| 585 kind: kind)); | 583 kind: kind)); |
| 586 } | 584 } |
| 587 pb.exportNames = exportNames; | 585 pb.exportNames = exportNames; |
| 588 return pb; | 586 return pb; |
| 589 } | 587 } |
| 590 | 588 |
| 591 /** | 589 /** |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 623 b.hasImplicitType = parameter.hasImplicitType; | 621 b.hasImplicitType = parameter.hasImplicitType; |
| 624 } | 622 } |
| 625 return b; | 623 return b; |
| 626 } | 624 } |
| 627 | 625 |
| 628 /** | 626 /** |
| 629 * Serialize the given [prefix] into an index into the references table. | 627 * Serialize the given [prefix] into an index into the references table. |
| 630 */ | 628 */ |
| 631 int serializePrefix(PrefixElement element) { | 629 int serializePrefix(PrefixElement element) { |
| 632 return referenceMap.putIfAbsent(element, () { | 630 return referenceMap.putIfAbsent(element, () { |
| 633 assert(unlinkedReferences.length == prelinkedReferences.length); | 631 assert(unlinkedReferences.length == linkedReferences.length); |
| 634 int index = unlinkedReferences.length; | 632 int index = unlinkedReferences.length; |
| 635 unlinkedReferences.add(new UnlinkedReferenceBuilder(name: element.name)); | 633 unlinkedReferences.add(new UnlinkedReferenceBuilder(name: element.name)); |
| 636 prelinkedReferences.add( | 634 linkedReferences |
| 637 new PrelinkedReferenceBuilder(kind: PrelinkedReferenceKind.prefix)); | 635 .add(new LinkedReferenceBuilder(kind: ReferenceKind.prefix)); |
| 638 return index; | 636 return index; |
| 639 }); | 637 }); |
| 640 } | 638 } |
| 641 | 639 |
| 642 /** | 640 /** |
| 643 * Serialize the given [typedefElement], creating an [UnlinkedTypedef]. | 641 * Serialize the given [typedefElement], creating an [UnlinkedTypedef]. |
| 644 */ | 642 */ |
| 645 UnlinkedTypedefBuilder serializeTypedef( | 643 UnlinkedTypedefBuilder serializeTypedef( |
| 646 FunctionTypeAliasElement typedefElement) { | 644 FunctionTypeAliasElement typedefElement) { |
| 647 UnlinkedTypedefBuilder b = new UnlinkedTypedefBuilder(); | 645 UnlinkedTypedefBuilder b = new UnlinkedTypedefBuilder(); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 684 LibraryElement dependentLibrary = element.library; | 682 LibraryElement dependentLibrary = element.library; |
| 685 if (dependentLibrary == null) { | 683 if (dependentLibrary == null) { |
| 686 assert(type.isDynamic); | 684 assert(type.isDynamic); |
| 687 if (type is UndefinedTypeImpl) { | 685 if (type is UndefinedTypeImpl) { |
| 688 b.reference = serializeUnresolvedReference(); | 686 b.reference = serializeUnresolvedReference(); |
| 689 } else { | 687 } else { |
| 690 b.reference = serializeDynamicReference(); | 688 b.reference = serializeDynamicReference(); |
| 691 } | 689 } |
| 692 } else { | 690 } else { |
| 693 b.reference = referenceMap.putIfAbsent(element, () { | 691 b.reference = referenceMap.putIfAbsent(element, () { |
| 694 assert(unlinkedReferences.length == prelinkedReferences.length); | 692 assert(unlinkedReferences.length == linkedReferences.length); |
| 695 CompilationUnitElement unitElement = | 693 CompilationUnitElement unitElement = |
| 696 element.getAncestor((Element e) => e is CompilationUnitElement); | 694 element.getAncestor((Element e) => e is CompilationUnitElement); |
| 697 int unit = dependentLibrary.units.indexOf(unitElement); | 695 int unit = dependentLibrary.units.indexOf(unitElement); |
| 698 assert(unit != -1); | 696 assert(unit != -1); |
| 699 int numTypeParameters = 0; | 697 int numTypeParameters = 0; |
| 700 if (element is TypeParameterizedElement) { | 698 if (element is TypeParameterizedElement) { |
| 701 numTypeParameters = element.typeParameters.length; | 699 numTypeParameters = element.typeParameters.length; |
| 702 } | 700 } |
| 703 // Figure out a prefix that may be used to refer to the given type. | 701 // Figure out a prefix that may be used to refer to the given type. |
| 704 // TODO(paulberry): to avoid subtle relinking inconsistencies we | 702 // TODO(paulberry): to avoid subtle relinking inconsistencies we |
| 705 // should use the actual prefix from the AST (a given type may be | 703 // should use the actual prefix from the AST (a given type may be |
| 706 // reachable via multiple prefixes), but sadly, this information is | 704 // reachable via multiple prefixes), but sadly, this information is |
| 707 // not recorded in the element model. | 705 // not recorded in the element model. |
| 708 int prefixReference = 0; | 706 int prefixReference = 0; |
| 709 PrefixElement prefix = prefixMap[element]; | 707 PrefixElement prefix = prefixMap[element]; |
| 710 if (prefix != null) { | 708 if (prefix != null) { |
| 711 prefixReference = serializePrefix(prefix); | 709 prefixReference = serializePrefix(prefix); |
| 712 } | 710 } |
| 713 int index = unlinkedReferences.length; | 711 int index = unlinkedReferences.length; |
| 714 unlinkedReferences.add(new UnlinkedReferenceBuilder( | 712 unlinkedReferences.add(new UnlinkedReferenceBuilder( |
| 715 name: element.name, prefixReference: prefixReference)); | 713 name: element.name, prefixReference: prefixReference)); |
| 716 prelinkedReferences.add(new PrelinkedReferenceBuilder( | 714 linkedReferences.add(new LinkedReferenceBuilder( |
| 717 dependency: serializeDependency(dependentLibrary), | 715 dependency: serializeDependency(dependentLibrary), |
| 718 kind: element is FunctionTypeAliasElement | 716 kind: element is FunctionTypeAliasElement |
| 719 ? PrelinkedReferenceKind.typedef | 717 ? ReferenceKind.typedef |
| 720 : PrelinkedReferenceKind.classOrEnum, | 718 : ReferenceKind.classOrEnum, |
| 721 unit: unit, | 719 unit: unit, |
| 722 numTypeParameters: numTypeParameters)); | 720 numTypeParameters: numTypeParameters)); |
| 723 return index; | 721 return index; |
| 724 }); | 722 }); |
| 725 } | 723 } |
| 726 List<DartType> typeArguments; | 724 List<DartType> typeArguments; |
| 727 if (type is InterfaceType) { | 725 if (type is InterfaceType) { |
| 728 typeArguments = type.typeArguments; | 726 typeArguments = type.typeArguments; |
| 729 } else if (type is FunctionType) { | 727 } else if (type is FunctionType) { |
| 730 typeArguments = type.typeArguments; | 728 typeArguments = type.typeArguments; |
| 731 } | 729 } |
| 732 if (typeArguments != null && | 730 if (typeArguments != null && |
| 733 typeArguments.any((DartType argument) => !argument.isDynamic)) { | 731 typeArguments.any((DartType argument) => !argument.isDynamic)) { |
| 734 b.typeArguments = typeArguments | 732 b.typeArguments = typeArguments |
| 735 .map((DartType t) => serializeTypeRef(t, context)) | 733 .map((DartType t) => serializeTypeRef(t, context)) |
| 736 .toList(); | 734 .toList(); |
| 737 } | 735 } |
| 738 } | 736 } |
| 739 return b; | 737 return b; |
| 740 } | 738 } |
| 741 | 739 |
| 742 /** | 740 /** |
| 743 * Return the index of the entry in the references table | 741 * Return the index of the entry in the references table |
| 744 * ([UnlinkedLibrary.references] and [PrelinkedLibrary.references]) used for | 742 * ([UnlinkedLibrary.references] and [LinkedLibrary.references]) used for |
| 745 * unresolved references. A new entry is added to the table if necessary to | 743 * unresolved references. A new entry is added to the table if necessary to |
| 746 * satisfy the request. | 744 * satisfy the request. |
| 747 */ | 745 */ |
| 748 int serializeUnresolvedReference() { | 746 int serializeUnresolvedReference() { |
| 749 // TODO(paulberry): in order for relinking to work, we need to record the | 747 // TODO(paulberry): in order for relinking to work, we need to record the |
| 750 // name and prefix of the unresolved symbol. This is not (yet) encoded in | 748 // name and prefix of the unresolved symbol. This is not (yet) encoded in |
| 751 // the element model. For the moment we use a name that can't possibly | 749 // the element model. For the moment we use a name that can't possibly |
| 752 // ever exist. | 750 // ever exist. |
| 753 if (unresolvedReferenceIndex == null) { | 751 if (unresolvedReferenceIndex == null) { |
| 754 assert(unlinkedReferences.length == prelinkedReferences.length); | 752 assert(unlinkedReferences.length == linkedReferences.length); |
| 755 unresolvedReferenceIndex = unlinkedReferences.length; | 753 unresolvedReferenceIndex = unlinkedReferences.length; |
| 756 unlinkedReferences | 754 unlinkedReferences |
| 757 .add(new UnlinkedReferenceBuilder(name: '*unresolved*')); | 755 .add(new UnlinkedReferenceBuilder(name: '*unresolved*')); |
| 758 prelinkedReferences.add(new PrelinkedReferenceBuilder( | 756 linkedReferences |
| 759 kind: PrelinkedReferenceKind.unresolved)); | 757 .add(new LinkedReferenceBuilder(kind: ReferenceKind.unresolved)); |
| 760 } | 758 } |
| 761 return unresolvedReferenceIndex; | 759 return unresolvedReferenceIndex; |
| 762 } | 760 } |
| 763 | 761 |
| 764 /** | 762 /** |
| 765 * Serialize the given [variable], creating an [UnlinkedVariable]. | 763 * Serialize the given [variable], creating an [UnlinkedVariable]. |
| 766 */ | 764 */ |
| 767 UnlinkedVariableBuilder serializeVariable(PropertyInducingElement variable) { | 765 UnlinkedVariableBuilder serializeVariable(PropertyInducingElement variable) { |
| 768 UnlinkedVariableBuilder b = new UnlinkedVariableBuilder(); | 766 UnlinkedVariableBuilder b = new UnlinkedVariableBuilder(); |
| 769 b.name = variable.name; | 767 b.name = variable.name; |
| 770 b.nameOffset = variable.nameOffset; | 768 b.nameOffset = variable.nameOffset; |
| 771 b.type = serializeTypeRef(variable.type, variable); | 769 b.type = serializeTypeRef(variable.type, variable); |
| 772 b.isStatic = variable.isStatic && variable.enclosingElement is ClassElement; | 770 b.isStatic = variable.isStatic && variable.enclosingElement is ClassElement; |
| 773 b.isFinal = variable.isFinal; | 771 b.isFinal = variable.isFinal; |
| 774 b.isConst = variable.isConst; | 772 b.isConst = variable.isConst; |
| 775 b.hasImplicitType = variable.hasImplicitType; | 773 b.hasImplicitType = variable.hasImplicitType; |
| 776 b.documentationComment = serializeDocumentation(variable); | 774 b.documentationComment = serializeDocumentation(variable); |
| 777 return b; | 775 return b; |
| 778 } | 776 } |
| 779 } | 777 } |
| OLD | NEW |