| 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/base.dart'; | |
| 13 import 'package:analyzer/src/summary/format.dart'; | 12 import 'package:analyzer/src/summary/format.dart'; |
| 14 | 13 |
| 15 /** | 14 /** |
| 16 * Serialize all the elements in [lib] to a summary using [ctx] as the context | 15 * 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. | 16 * for building the summary, and using [typeProvider] to find built-in types. |
| 18 */ | 17 */ |
| 19 LibrarySerializationResult serializeLibrary( | 18 LibrarySerializationResult serializeLibrary( |
| 20 BuilderContext ctx, LibraryElement lib, TypeProvider typeProvider) { | 19 LibraryElement lib, TypeProvider typeProvider) { |
| 21 var serializer = new _LibrarySerializer(ctx, lib, typeProvider); | 20 var serializer = new _LibrarySerializer(lib, typeProvider); |
| 22 PrelinkedLibraryBuilder prelinked = serializer.serializeLibrary(); | 21 PrelinkedLibraryBuilder prelinked = serializer.serializeLibrary(); |
| 23 return new LibrarySerializationResult( | 22 return new LibrarySerializationResult( |
| 24 prelinked, serializer.unlinkedUnits, serializer.unitUris); | 23 prelinked, serializer.unlinkedUnits, serializer.unitUris); |
| 25 } | 24 } |
| 26 | 25 |
| 27 /** | 26 /** |
| 28 * Data structure holding the result of serializing a [LibraryElement]. | 27 * Data structure holding the result of serializing a [LibraryElement]. |
| 29 */ | 28 */ |
| 30 class LibrarySerializationResult { | 29 class LibrarySerializationResult { |
| 31 /** | 30 /** |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 int unresolvedReferenceIndex = null; | 126 int unresolvedReferenceIndex = null; |
| 128 | 127 |
| 129 /** | 128 /** |
| 130 * Set of libraries which have been seen so far while visiting the transitive | 129 * Set of libraries which have been seen so far while visiting the transitive |
| 131 * closure of exports. | 130 * closure of exports. |
| 132 */ | 131 */ |
| 133 final Set<LibraryElement> librariesAddedToTransitiveExportClosure = | 132 final Set<LibraryElement> librariesAddedToTransitiveExportClosure = |
| 134 new Set<LibraryElement>(); | 133 new Set<LibraryElement>(); |
| 135 | 134 |
| 136 /** | 135 /** |
| 137 * [BuilderContext] used to serialize the output summary. | |
| 138 */ | |
| 139 final BuilderContext ctx; | |
| 140 | |
| 141 /** | |
| 142 * Map from imported element to the prefix which may be used to refer to that | 136 * Map from imported element to the prefix which may be used to refer to that |
| 143 * element; elements for which no prefix is needed are absent from this map. | 137 * element; elements for which no prefix is needed are absent from this map. |
| 144 */ | 138 */ |
| 145 final Map<Element, PrefixElement> prefixMap = <Element, PrefixElement>{}; | 139 final Map<Element, PrefixElement> prefixMap = <Element, PrefixElement>{}; |
| 146 | 140 |
| 147 _LibrarySerializer(this.ctx, this.libraryElement, this.typeProvider) { | 141 _LibrarySerializer(this.libraryElement, this.typeProvider) { |
| 148 dependencies.add(encodePrelinkedDependency(ctx)); | 142 dependencies.add(encodePrelinkedDependency()); |
| 149 dependencyMap[libraryElement] = 0; | 143 dependencyMap[libraryElement] = 0; |
| 150 } | 144 } |
| 151 | 145 |
| 152 /** | 146 /** |
| 153 * Retrieve the library element for `dart:core`. | 147 * Retrieve the library element for `dart:core`. |
| 154 */ | 148 */ |
| 155 LibraryElement get coreLibrary => typeProvider.objectType.element.library; | 149 LibraryElement get coreLibrary => typeProvider.objectType.element.library; |
| 156 | 150 |
| 157 /** | 151 /** |
| 158 * Add all classes, enums, typedefs, executables, and top level variables | 152 * Add all classes, enums, typedefs, executables, and top level variables |
| 159 * from the given compilation unit [element] to the library summary. | 153 * from the given compilation unit [element] to the library summary. |
| 160 * [unitNum] indicates the ordinal position of this compilation unit in the | 154 * [unitNum] indicates the ordinal position of this compilation unit in the |
| 161 * library. | 155 * library. |
| 162 */ | 156 */ |
| 163 void addCompilationUnitElements(CompilationUnitElement element, int unitNum) { | 157 void addCompilationUnitElements(CompilationUnitElement element, int unitNum) { |
| 164 UnlinkedUnitBuilder b = new UnlinkedUnitBuilder(ctx); | 158 UnlinkedUnitBuilder b = new UnlinkedUnitBuilder(); |
| 165 referenceMap.clear(); | 159 referenceMap.clear(); |
| 166 unlinkedReferences = <UnlinkedReferenceBuilder>[ | 160 unlinkedReferences = <UnlinkedReferenceBuilder>[encodeUnlinkedReference()]; |
| 167 encodeUnlinkedReference(ctx) | |
| 168 ]; | |
| 169 prelinkedReferences = <PrelinkedReferenceBuilder>[ | 161 prelinkedReferences = <PrelinkedReferenceBuilder>[ |
| 170 encodePrelinkedReference(ctx, kind: PrelinkedReferenceKind.classOrEnum) | 162 encodePrelinkedReference(kind: PrelinkedReferenceKind.classOrEnum) |
| 171 ]; | 163 ]; |
| 172 List<UnlinkedPublicNameBuilder> names = <UnlinkedPublicNameBuilder>[]; | 164 List<UnlinkedPublicNameBuilder> names = <UnlinkedPublicNameBuilder>[]; |
| 173 for (PropertyAccessorElement accessor in element.accessors) { | 165 for (PropertyAccessorElement accessor in element.accessors) { |
| 174 if (accessor.isPublic) { | 166 if (accessor.isPublic) { |
| 175 names.add(encodeUnlinkedPublicName(ctx, | 167 names.add(encodeUnlinkedPublicName( |
| 176 kind: PrelinkedReferenceKind.other, | 168 kind: PrelinkedReferenceKind.other, |
| 177 name: accessor.name, | 169 name: accessor.name, |
| 178 numTypeParameters: accessor.typeParameters.length)); | 170 numTypeParameters: accessor.typeParameters.length)); |
| 179 } | 171 } |
| 180 } | 172 } |
| 181 for (ClassElement cls in element.types) { | 173 for (ClassElement cls in element.types) { |
| 182 if (cls.isPublic) { | 174 if (cls.isPublic) { |
| 183 names.add(encodeUnlinkedPublicName(ctx, | 175 names.add(encodeUnlinkedPublicName( |
| 184 kind: PrelinkedReferenceKind.classOrEnum, | 176 kind: PrelinkedReferenceKind.classOrEnum, |
| 185 name: cls.name, | 177 name: cls.name, |
| 186 numTypeParameters: cls.typeParameters.length)); | 178 numTypeParameters: cls.typeParameters.length)); |
| 187 } | 179 } |
| 188 } | 180 } |
| 189 for (ClassElement enm in element.enums) { | 181 for (ClassElement enm in element.enums) { |
| 190 if (enm.isPublic) { | 182 if (enm.isPublic) { |
| 191 names.add(encodeUnlinkedPublicName(ctx, | 183 names.add(encodeUnlinkedPublicName( |
| 192 kind: PrelinkedReferenceKind.classOrEnum, name: enm.name)); | 184 kind: PrelinkedReferenceKind.classOrEnum, name: enm.name)); |
| 193 } | 185 } |
| 194 } | 186 } |
| 195 for (FunctionElement function in element.functions) { | 187 for (FunctionElement function in element.functions) { |
| 196 if (function.isPublic) { | 188 if (function.isPublic) { |
| 197 names.add(encodeUnlinkedPublicName(ctx, | 189 names.add(encodeUnlinkedPublicName( |
| 198 kind: PrelinkedReferenceKind.other, | 190 kind: PrelinkedReferenceKind.other, |
| 199 name: function.name, | 191 name: function.name, |
| 200 numTypeParameters: function.typeParameters.length)); | 192 numTypeParameters: function.typeParameters.length)); |
| 201 } | 193 } |
| 202 } | 194 } |
| 203 for (FunctionTypeAliasElement typedef in element.functionTypeAliases) { | 195 for (FunctionTypeAliasElement typedef in element.functionTypeAliases) { |
| 204 if (typedef.isPublic) { | 196 if (typedef.isPublic) { |
| 205 names.add(encodeUnlinkedPublicName(ctx, | 197 names.add(encodeUnlinkedPublicName( |
| 206 kind: PrelinkedReferenceKind.typedef, | 198 kind: PrelinkedReferenceKind.typedef, |
| 207 name: typedef.name, | 199 name: typedef.name, |
| 208 numTypeParameters: typedef.typeParameters.length)); | 200 numTypeParameters: typedef.typeParameters.length)); |
| 209 } | 201 } |
| 210 } | 202 } |
| 211 if (unitNum == 0) { | 203 if (unitNum == 0) { |
| 212 if (libraryElement.name.isNotEmpty) { | 204 if (libraryElement.name.isNotEmpty) { |
| 213 b.libraryName = libraryElement.name; | 205 b.libraryName = libraryElement.name; |
| 214 b.libraryNameOffset = libraryElement.nameOffset; | 206 b.libraryNameOffset = libraryElement.nameOffset; |
| 215 b.libraryNameLength = libraryElement.nameLength; | 207 b.libraryNameLength = libraryElement.nameLength; |
| 216 b.libraryDocumentationComment = serializeDocumentation(libraryElement); | 208 b.libraryDocumentationComment = serializeDocumentation(libraryElement); |
| 217 } | 209 } |
| 218 b.publicNamespace = encodeUnlinkedPublicNamespace(ctx, | 210 b.publicNamespace = encodeUnlinkedPublicNamespace( |
| 219 exports: libraryElement.exports.map(serializeExportPublic).toList(), | 211 exports: libraryElement.exports.map(serializeExportPublic).toList(), |
| 220 parts: libraryElement.parts | 212 parts: libraryElement.parts |
| 221 .map((CompilationUnitElement e) => e.uri) | 213 .map((CompilationUnitElement e) => e.uri) |
| 222 .toList(), | 214 .toList(), |
| 223 names: names); | 215 names: names); |
| 224 b.exports = libraryElement.exports.map(serializeExportNonPublic).toList(); | 216 b.exports = libraryElement.exports.map(serializeExportNonPublic).toList(); |
| 225 b.imports = libraryElement.imports.map(serializeImport).toList(); | 217 b.imports = libraryElement.imports.map(serializeImport).toList(); |
| 226 b.parts = libraryElement.parts | 218 b.parts = libraryElement.parts |
| 227 .map((CompilationUnitElement e) => | 219 .map((CompilationUnitElement e) => |
| 228 encodeUnlinkedPart(ctx, uriOffset: e.uriOffset, uriEnd: e.uriEnd)) | 220 encodeUnlinkedPart(uriOffset: e.uriOffset, uriEnd: e.uriEnd)) |
| 229 .toList(); | 221 .toList(); |
| 230 } else { | 222 } else { |
| 231 // TODO(paulberry): we need to figure out a way to record library, part, | 223 // TODO(paulberry): we need to figure out a way to record library, part, |
| 232 // import, and export declarations that appear in non-defining | 224 // import, and export declarations that appear in non-defining |
| 233 // compilation units (even though such declarations are prohibited by the | 225 // compilation units (even though such declarations are prohibited by the |
| 234 // language), so that if the user makes code changes that cause a | 226 // language), so that if the user makes code changes that cause a |
| 235 // non-defining compilation unit to become a defining compilation unit, | 227 // non-defining compilation unit to become a defining compilation unit, |
| 236 // we can create a correct summary by simply re-linking. | 228 // we can create a correct summary by simply re-linking. |
| 237 b.publicNamespace = encodeUnlinkedPublicNamespace(ctx, names: names); | 229 b.publicNamespace = encodeUnlinkedPublicNamespace(names: names); |
| 238 } | 230 } |
| 239 b.classes = element.types.map(serializeClass).toList(); | 231 b.classes = element.types.map(serializeClass).toList(); |
| 240 b.enums = element.enums.map(serializeEnum).toList(); | 232 b.enums = element.enums.map(serializeEnum).toList(); |
| 241 b.typedefs = element.functionTypeAliases.map(serializeTypedef).toList(); | 233 b.typedefs = element.functionTypeAliases.map(serializeTypedef).toList(); |
| 242 List<UnlinkedExecutableBuilder> executables = | 234 List<UnlinkedExecutableBuilder> executables = |
| 243 element.functions.map(serializeExecutable).toList(); | 235 element.functions.map(serializeExecutable).toList(); |
| 244 for (PropertyAccessorElement accessor in element.accessors) { | 236 for (PropertyAccessorElement accessor in element.accessors) { |
| 245 if (!accessor.isSynthetic) { | 237 if (!accessor.isSynthetic) { |
| 246 executables.add(serializeExecutable(accessor)); | 238 executables.add(serializeExecutable(accessor)); |
| 247 } | 239 } |
| 248 } | 240 } |
| 249 b.executables = executables; | 241 b.executables = executables; |
| 250 List<UnlinkedVariableBuilder> variables = <UnlinkedVariableBuilder>[]; | 242 List<UnlinkedVariableBuilder> variables = <UnlinkedVariableBuilder>[]; |
| 251 for (PropertyAccessorElement accessor in element.accessors) { | 243 for (PropertyAccessorElement accessor in element.accessors) { |
| 252 if (accessor.isSynthetic && accessor.isGetter) { | 244 if (accessor.isSynthetic && accessor.isGetter) { |
| 253 PropertyInducingElement variable = accessor.variable; | 245 PropertyInducingElement variable = accessor.variable; |
| 254 if (variable != null) { | 246 if (variable != null) { |
| 255 assert(!variable.isSynthetic); | 247 assert(!variable.isSynthetic); |
| 256 variables.add(serializeVariable(variable)); | 248 variables.add(serializeVariable(variable)); |
| 257 } | 249 } |
| 258 } | 250 } |
| 259 } | 251 } |
| 260 b.variables = variables; | 252 b.variables = variables; |
| 261 b.references = unlinkedReferences; | 253 b.references = unlinkedReferences; |
| 262 unlinkedUnits.add(b); | 254 unlinkedUnits.add(b); |
| 263 prelinkedUnits | 255 prelinkedUnits.add(encodePrelinkedUnit(references: prelinkedReferences)); |
| 264 .add(encodePrelinkedUnit(ctx, references: prelinkedReferences)); | |
| 265 unitUris.add(element.source.uri.toString()); | 256 unitUris.add(element.source.uri.toString()); |
| 266 unlinkedReferences = null; | 257 unlinkedReferences = null; |
| 267 prelinkedReferences = null; | 258 prelinkedReferences = null; |
| 268 } | 259 } |
| 269 | 260 |
| 270 /** | 261 /** |
| 271 * Add [exportedLibrary] (and the transitive closure of all libraries it | 262 * Add [exportedLibrary] (and the transitive closure of all libraries it |
| 272 * exports) to the dependency table ([PrelinkedLibrary.dependencies]). | 263 * exports) to the dependency table ([PrelinkedLibrary.dependencies]). |
| 273 */ | 264 */ |
| 274 void addTransitiveExportClosure(LibraryElement exportedLibrary) { | 265 void addTransitiveExportClosure(LibraryElement exportedLibrary) { |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 } | 332 } |
| 342 context = context.enclosingElement; | 333 context = context.enclosingElement; |
| 343 } | 334 } |
| 344 throw new StateError('Unbound type parameter $type'); | 335 throw new StateError('Unbound type parameter $type'); |
| 345 } | 336 } |
| 346 | 337 |
| 347 /** | 338 /** |
| 348 * Serialize the given [classElement], creating an [UnlinkedClass]. | 339 * Serialize the given [classElement], creating an [UnlinkedClass]. |
| 349 */ | 340 */ |
| 350 UnlinkedClassBuilder serializeClass(ClassElement classElement) { | 341 UnlinkedClassBuilder serializeClass(ClassElement classElement) { |
| 351 UnlinkedClassBuilder b = new UnlinkedClassBuilder(ctx); | 342 UnlinkedClassBuilder b = new UnlinkedClassBuilder(); |
| 352 b.name = classElement.name; | 343 b.name = classElement.name; |
| 353 b.nameOffset = classElement.nameOffset; | 344 b.nameOffset = classElement.nameOffset; |
| 354 b.typeParameters = | 345 b.typeParameters = |
| 355 classElement.typeParameters.map(serializeTypeParam).toList(); | 346 classElement.typeParameters.map(serializeTypeParam).toList(); |
| 356 if (classElement.supertype == null) { | 347 if (classElement.supertype == null) { |
| 357 b.hasNoSupertype = true; | 348 b.hasNoSupertype = true; |
| 358 } else if (!classElement.supertype.isObject) { | 349 } else if (!classElement.supertype.isObject) { |
| 359 b.supertype = serializeTypeRef(classElement.supertype, classElement); | 350 b.supertype = serializeTypeRef(classElement.supertype, classElement); |
| 360 } | 351 } |
| 361 b.mixins = classElement.mixins | 352 b.mixins = classElement.mixins |
| (...skipping 28 matching lines...) Expand all Loading... |
| 390 b.isMixinApplication = classElement.isMixinApplication; | 381 b.isMixinApplication = classElement.isMixinApplication; |
| 391 b.documentationComment = serializeDocumentation(classElement); | 382 b.documentationComment = serializeDocumentation(classElement); |
| 392 return b; | 383 return b; |
| 393 } | 384 } |
| 394 | 385 |
| 395 /** | 386 /** |
| 396 * Serialize the given [combinator] into an [UnlinkedCombinator]. | 387 * Serialize the given [combinator] into an [UnlinkedCombinator]. |
| 397 */ | 388 */ |
| 398 UnlinkedCombinatorBuilder serializeCombinator( | 389 UnlinkedCombinatorBuilder serializeCombinator( |
| 399 NamespaceCombinator combinator) { | 390 NamespaceCombinator combinator) { |
| 400 UnlinkedCombinatorBuilder b = new UnlinkedCombinatorBuilder(ctx); | 391 UnlinkedCombinatorBuilder b = new UnlinkedCombinatorBuilder(); |
| 401 if (combinator is ShowElementCombinator) { | 392 if (combinator is ShowElementCombinator) { |
| 402 b.shows = combinator.shownNames; | 393 b.shows = combinator.shownNames; |
| 403 } else if (combinator is HideElementCombinator) { | 394 } else if (combinator is HideElementCombinator) { |
| 404 b.hides = combinator.hiddenNames; | 395 b.hides = combinator.hiddenNames; |
| 405 } | 396 } |
| 406 return b; | 397 return b; |
| 407 } | 398 } |
| 408 | 399 |
| 409 /** | 400 /** |
| 410 * Return the index of the entry in the dependency table | 401 * Return the index of the entry in the dependency table |
| 411 * ([PrelinkedLibrary.dependencies]) for the given [dependentLibrary]. A new | 402 * ([PrelinkedLibrary.dependencies]) for the given [dependentLibrary]. A new |
| 412 * entry is added to the table if necessary to satisfy the request. | 403 * entry is added to the table if necessary to satisfy the request. |
| 413 */ | 404 */ |
| 414 int serializeDependency(LibraryElement dependentLibrary) { | 405 int serializeDependency(LibraryElement dependentLibrary) { |
| 415 return dependencyMap.putIfAbsent(dependentLibrary, () { | 406 return dependencyMap.putIfAbsent(dependentLibrary, () { |
| 416 int index = dependencies.length; | 407 int index = dependencies.length; |
| 417 dependencies.add(encodePrelinkedDependency(ctx, | 408 dependencies.add(encodePrelinkedDependency( |
| 418 uri: dependentLibrary.source.uri.toString())); | 409 uri: dependentLibrary.source.uri.toString())); |
| 419 return index; | 410 return index; |
| 420 }); | 411 }); |
| 421 } | 412 } |
| 422 | 413 |
| 423 /** | 414 /** |
| 424 * Serialize documentation from the given [element], creating an | 415 * Serialize documentation from the given [element], creating an |
| 425 * [UnlinkedDocumentationComment]. | 416 * [UnlinkedDocumentationComment]. |
| 426 * | 417 * |
| 427 * If [element] has no documentation, `null` is returned. | 418 * If [element] has no documentation, `null` is returned. |
| 428 */ | 419 */ |
| 429 UnlinkedDocumentationCommentBuilder serializeDocumentation(Element element) { | 420 UnlinkedDocumentationCommentBuilder serializeDocumentation(Element element) { |
| 430 if (element.documentationComment == null) { | 421 if (element.documentationComment == null) { |
| 431 return null; | 422 return null; |
| 432 } | 423 } |
| 433 return encodeUnlinkedDocumentationComment(ctx, | 424 return encodeUnlinkedDocumentationComment( |
| 434 text: element.documentationComment, | 425 text: element.documentationComment, |
| 435 offset: element.docRange.offset, | 426 offset: element.docRange.offset, |
| 436 length: element.docRange.length); | 427 length: element.docRange.length); |
| 437 } | 428 } |
| 438 | 429 |
| 439 /** | 430 /** |
| 440 * Return the index of the entry in the references table | 431 * Return the index of the entry in the references table |
| 441 * ([UnlinkedLibrary.references] and [PrelinkedLibrary.references]) | 432 * ([UnlinkedLibrary.references] and [PrelinkedLibrary.references]) |
| 442 * representing the pseudo-type `dynamic`. | 433 * representing the pseudo-type `dynamic`. |
| 443 */ | 434 */ |
| 444 int serializeDynamicReference() => 0; | 435 int serializeDynamicReference() => 0; |
| 445 | 436 |
| 446 /** | 437 /** |
| 447 * Serialize the given [enumElement], creating an [UnlinkedEnum]. | 438 * Serialize the given [enumElement], creating an [UnlinkedEnum]. |
| 448 */ | 439 */ |
| 449 UnlinkedEnumBuilder serializeEnum(ClassElement enumElement) { | 440 UnlinkedEnumBuilder serializeEnum(ClassElement enumElement) { |
| 450 UnlinkedEnumBuilder b = new UnlinkedEnumBuilder(ctx); | 441 UnlinkedEnumBuilder b = new UnlinkedEnumBuilder(); |
| 451 b.name = enumElement.name; | 442 b.name = enumElement.name; |
| 452 b.nameOffset = enumElement.nameOffset; | 443 b.nameOffset = enumElement.nameOffset; |
| 453 List<UnlinkedEnumValueBuilder> values = <UnlinkedEnumValueBuilder>[]; | 444 List<UnlinkedEnumValueBuilder> values = <UnlinkedEnumValueBuilder>[]; |
| 454 for (FieldElement field in enumElement.fields) { | 445 for (FieldElement field in enumElement.fields) { |
| 455 if (field.isConst && field.type.element == enumElement) { | 446 if (field.isConst && field.type.element == enumElement) { |
| 456 values.add(encodeUnlinkedEnumValue(ctx, | 447 values.add(encodeUnlinkedEnumValue( |
| 457 name: field.name, | 448 name: field.name, |
| 458 nameOffset: field.nameOffset, | 449 nameOffset: field.nameOffset, |
| 459 documentationComment: serializeDocumentation(field))); | 450 documentationComment: serializeDocumentation(field))); |
| 460 } | 451 } |
| 461 } | 452 } |
| 462 b.values = values; | 453 b.values = values; |
| 463 b.documentationComment = serializeDocumentation(enumElement); | 454 b.documentationComment = serializeDocumentation(enumElement); |
| 464 return b; | 455 return b; |
| 465 } | 456 } |
| 466 | 457 |
| 467 /** | 458 /** |
| 468 * Serialize the given [executableElement], creating an [UnlinkedExecutable]. | 459 * Serialize the given [executableElement], creating an [UnlinkedExecutable]. |
| 469 */ | 460 */ |
| 470 UnlinkedExecutableBuilder serializeExecutable( | 461 UnlinkedExecutableBuilder serializeExecutable( |
| 471 ExecutableElement executableElement) { | 462 ExecutableElement executableElement) { |
| 472 UnlinkedExecutableBuilder b = new UnlinkedExecutableBuilder(ctx); | 463 UnlinkedExecutableBuilder b = new UnlinkedExecutableBuilder(); |
| 473 b.name = executableElement.name; | 464 b.name = executableElement.name; |
| 474 b.nameOffset = executableElement.nameOffset; | 465 b.nameOffset = executableElement.nameOffset; |
| 475 if (executableElement is! ConstructorElement && | 466 if (executableElement is! ConstructorElement && |
| 476 !executableElement.type.returnType.isVoid) { | 467 !executableElement.type.returnType.isVoid) { |
| 477 b.returnType = serializeTypeRef( | 468 b.returnType = serializeTypeRef( |
| 478 executableElement.type.returnType, executableElement); | 469 executableElement.type.returnType, executableElement); |
| 479 } | 470 } |
| 480 b.typeParameters = | 471 b.typeParameters = |
| 481 executableElement.typeParameters.map(serializeTypeParam).toList(); | 472 executableElement.typeParameters.map(serializeTypeParam).toList(); |
| 482 b.parameters = | 473 b.parameters = |
| (...skipping 18 matching lines...) Expand all Loading... |
| 501 b.isExternal = executableElement.isExternal; | 492 b.isExternal = executableElement.isExternal; |
| 502 b.documentationComment = serializeDocumentation(executableElement); | 493 b.documentationComment = serializeDocumentation(executableElement); |
| 503 return b; | 494 return b; |
| 504 } | 495 } |
| 505 | 496 |
| 506 /** | 497 /** |
| 507 * Serialize the given [exportElement] into an [UnlinkedExportNonPublic]. | 498 * Serialize the given [exportElement] into an [UnlinkedExportNonPublic]. |
| 508 */ | 499 */ |
| 509 UnlinkedExportNonPublicBuilder serializeExportNonPublic( | 500 UnlinkedExportNonPublicBuilder serializeExportNonPublic( |
| 510 ExportElement exportElement) { | 501 ExportElement exportElement) { |
| 511 UnlinkedExportNonPublicBuilder b = new UnlinkedExportNonPublicBuilder(ctx); | 502 UnlinkedExportNonPublicBuilder b = new UnlinkedExportNonPublicBuilder(); |
| 512 b.offset = exportElement.nameOffset; | 503 b.offset = exportElement.nameOffset; |
| 513 b.uriOffset = exportElement.uriOffset; | 504 b.uriOffset = exportElement.uriOffset; |
| 514 b.uriEnd = exportElement.uriEnd; | 505 b.uriEnd = exportElement.uriEnd; |
| 515 return b; | 506 return b; |
| 516 } | 507 } |
| 517 | 508 |
| 518 /** | 509 /** |
| 519 * Serialize the given [exportElement] into an [UnlinkedExportPublic]. | 510 * Serialize the given [exportElement] into an [UnlinkedExportPublic]. |
| 520 */ | 511 */ |
| 521 UnlinkedExportPublicBuilder serializeExportPublic( | 512 UnlinkedExportPublicBuilder serializeExportPublic( |
| 522 ExportElement exportElement) { | 513 ExportElement exportElement) { |
| 523 UnlinkedExportPublicBuilder b = new UnlinkedExportPublicBuilder(ctx); | 514 UnlinkedExportPublicBuilder b = new UnlinkedExportPublicBuilder(); |
| 524 b.uri = exportElement.uri; | 515 b.uri = exportElement.uri; |
| 525 b.combinators = exportElement.combinators.map(serializeCombinator).toList(); | 516 b.combinators = exportElement.combinators.map(serializeCombinator).toList(); |
| 526 return b; | 517 return b; |
| 527 } | 518 } |
| 528 | 519 |
| 529 /** | 520 /** |
| 530 * Serialize the given [importElement] yielding an [UnlinkedImportBuilder]. | 521 * Serialize the given [importElement] yielding an [UnlinkedImportBuilder]. |
| 531 * Also, add pre-linked information about it to the [prelinkedImports] list. | 522 * Also, add pre-linked information about it to the [prelinkedImports] list. |
| 532 */ | 523 */ |
| 533 UnlinkedImportBuilder serializeImport(ImportElement importElement) { | 524 UnlinkedImportBuilder serializeImport(ImportElement importElement) { |
| 534 UnlinkedImportBuilder b = new UnlinkedImportBuilder(ctx); | 525 UnlinkedImportBuilder b = new UnlinkedImportBuilder(); |
| 535 b.isDeferred = importElement.isDeferred; | 526 b.isDeferred = importElement.isDeferred; |
| 536 b.offset = importElement.nameOffset; | 527 b.offset = importElement.nameOffset; |
| 537 b.combinators = importElement.combinators.map(serializeCombinator).toList(); | 528 b.combinators = importElement.combinators.map(serializeCombinator).toList(); |
| 538 if (importElement.prefix != null) { | 529 if (importElement.prefix != null) { |
| 539 b.prefixReference = serializePrefix(importElement.prefix); | 530 b.prefixReference = serializePrefix(importElement.prefix); |
| 540 b.prefixOffset = importElement.prefix.nameOffset; | 531 b.prefixOffset = importElement.prefix.nameOffset; |
| 541 } | 532 } |
| 542 if (importElement.isSynthetic) { | 533 if (importElement.isSynthetic) { |
| 543 b.isImplicit = true; | 534 b.isImplicit = true; |
| 544 } else { | 535 } else { |
| 545 b.uri = importElement.uri; | 536 b.uri = importElement.uri; |
| 546 b.uriOffset = importElement.uriOffset; | 537 b.uriOffset = importElement.uriOffset; |
| 547 b.uriEnd = importElement.uriEnd; | 538 b.uriEnd = importElement.uriEnd; |
| 548 } | 539 } |
| 549 addTransitiveExportClosure(importElement.importedLibrary); | 540 addTransitiveExportClosure(importElement.importedLibrary); |
| 550 prelinkedImports.add(serializeDependency(importElement.importedLibrary)); | 541 prelinkedImports.add(serializeDependency(importElement.importedLibrary)); |
| 551 return b; | 542 return b; |
| 552 } | 543 } |
| 553 | 544 |
| 554 /** | 545 /** |
| 555 * Serialize the whole library element into a [PrelinkedLibrary]. Should be | 546 * Serialize the whole library element into a [PrelinkedLibrary]. Should be |
| 556 * called exactly once for each instance of [_LibrarySerializer]. | 547 * called exactly once for each instance of [_LibrarySerializer]. |
| 557 * | 548 * |
| 558 * The unlinked compilation units are stored in [unlinkedUnits], and their | 549 * The unlinked compilation units are stored in [unlinkedUnits], and their |
| 559 * absolute URIs are stored in [unitUris]. | 550 * absolute URIs are stored in [unitUris]. |
| 560 */ | 551 */ |
| 561 PrelinkedLibraryBuilder serializeLibrary() { | 552 PrelinkedLibraryBuilder serializeLibrary() { |
| 562 computePrefixMap(); | 553 computePrefixMap(); |
| 563 PrelinkedLibraryBuilder pb = new PrelinkedLibraryBuilder(ctx); | 554 PrelinkedLibraryBuilder pb = new PrelinkedLibraryBuilder(); |
| 564 addCompilationUnitElements(libraryElement.definingCompilationUnit, 0); | 555 addCompilationUnitElements(libraryElement.definingCompilationUnit, 0); |
| 565 for (int i = 0; i < libraryElement.parts.length; i++) { | 556 for (int i = 0; i < libraryElement.parts.length; i++) { |
| 566 addCompilationUnitElements(libraryElement.parts[i], i + 1); | 557 addCompilationUnitElements(libraryElement.parts[i], i + 1); |
| 567 } | 558 } |
| 568 pb.units = prelinkedUnits; | 559 pb.units = prelinkedUnits; |
| 569 pb.dependencies = dependencies; | 560 pb.dependencies = dependencies; |
| 570 pb.importDependencies = prelinkedImports; | 561 pb.importDependencies = prelinkedImports; |
| 571 return pb; | 562 return pb; |
| 572 } | 563 } |
| 573 | 564 |
| 574 /** | 565 /** |
| 575 * Serialize the given [parameter] into an [UnlinkedParam]. | 566 * Serialize the given [parameter] into an [UnlinkedParam]. |
| 576 */ | 567 */ |
| 577 UnlinkedParamBuilder serializeParam(ParameterElement parameter, | 568 UnlinkedParamBuilder serializeParam(ParameterElement parameter, |
| 578 [Element context]) { | 569 [Element context]) { |
| 579 context ??= parameter; | 570 context ??= parameter; |
| 580 UnlinkedParamBuilder b = new UnlinkedParamBuilder(ctx); | 571 UnlinkedParamBuilder b = new UnlinkedParamBuilder(); |
| 581 b.name = parameter.name; | 572 b.name = parameter.name; |
| 582 b.nameOffset = parameter.nameOffset; | 573 b.nameOffset = parameter.nameOffset; |
| 583 switch (parameter.parameterKind) { | 574 switch (parameter.parameterKind) { |
| 584 case ParameterKind.REQUIRED: | 575 case ParameterKind.REQUIRED: |
| 585 b.kind = UnlinkedParamKind.required; | 576 b.kind = UnlinkedParamKind.required; |
| 586 break; | 577 break; |
| 587 case ParameterKind.POSITIONAL: | 578 case ParameterKind.POSITIONAL: |
| 588 b.kind = UnlinkedParamKind.positional; | 579 b.kind = UnlinkedParamKind.positional; |
| 589 break; | 580 break; |
| 590 case ParameterKind.NAMED: | 581 case ParameterKind.NAMED: |
| (...skipping 17 matching lines...) Expand all Loading... |
| 608 return b; | 599 return b; |
| 609 } | 600 } |
| 610 | 601 |
| 611 /** | 602 /** |
| 612 * Serialize the given [prefix] into an index into the references table. | 603 * Serialize the given [prefix] into an index into the references table. |
| 613 */ | 604 */ |
| 614 int serializePrefix(PrefixElement element) { | 605 int serializePrefix(PrefixElement element) { |
| 615 return referenceMap.putIfAbsent(element, () { | 606 return referenceMap.putIfAbsent(element, () { |
| 616 assert(unlinkedReferences.length == prelinkedReferences.length); | 607 assert(unlinkedReferences.length == prelinkedReferences.length); |
| 617 int index = unlinkedReferences.length; | 608 int index = unlinkedReferences.length; |
| 618 unlinkedReferences.add(encodeUnlinkedReference(ctx, name: element.name)); | 609 unlinkedReferences.add(encodeUnlinkedReference(name: element.name)); |
| 619 prelinkedReferences.add( | 610 prelinkedReferences |
| 620 encodePrelinkedReference(ctx, kind: PrelinkedReferenceKind.prefix)); | 611 .add(encodePrelinkedReference(kind: PrelinkedReferenceKind.prefix)); |
| 621 return index; | 612 return index; |
| 622 }); | 613 }); |
| 623 } | 614 } |
| 624 | 615 |
| 625 /** | 616 /** |
| 626 * Serialize the given [typedefElement], creating an [UnlinkedTypedef]. | 617 * Serialize the given [typedefElement], creating an [UnlinkedTypedef]. |
| 627 */ | 618 */ |
| 628 UnlinkedTypedefBuilder serializeTypedef( | 619 UnlinkedTypedefBuilder serializeTypedef( |
| 629 FunctionTypeAliasElement typedefElement) { | 620 FunctionTypeAliasElement typedefElement) { |
| 630 UnlinkedTypedefBuilder b = new UnlinkedTypedefBuilder(ctx); | 621 UnlinkedTypedefBuilder b = new UnlinkedTypedefBuilder(); |
| 631 b.name = typedefElement.name; | 622 b.name = typedefElement.name; |
| 632 b.nameOffset = typedefElement.nameOffset; | 623 b.nameOffset = typedefElement.nameOffset; |
| 633 b.typeParameters = | 624 b.typeParameters = |
| 634 typedefElement.typeParameters.map(serializeTypeParam).toList(); | 625 typedefElement.typeParameters.map(serializeTypeParam).toList(); |
| 635 if (!typedefElement.returnType.isVoid) { | 626 if (!typedefElement.returnType.isVoid) { |
| 636 b.returnType = | 627 b.returnType = |
| 637 serializeTypeRef(typedefElement.returnType, typedefElement); | 628 serializeTypeRef(typedefElement.returnType, typedefElement); |
| 638 } | 629 } |
| 639 b.parameters = typedefElement.parameters.map(serializeParam).toList(); | 630 b.parameters = typedefElement.parameters.map(serializeParam).toList(); |
| 640 b.documentationComment = serializeDocumentation(typedefElement); | 631 b.documentationComment = serializeDocumentation(typedefElement); |
| 641 return b; | 632 return b; |
| 642 } | 633 } |
| 643 | 634 |
| 644 /** | 635 /** |
| 645 * Serialize the given [typeParameter] into an [UnlinkedTypeParam]. | 636 * Serialize the given [typeParameter] into an [UnlinkedTypeParam]. |
| 646 */ | 637 */ |
| 647 UnlinkedTypeParamBuilder serializeTypeParam( | 638 UnlinkedTypeParamBuilder serializeTypeParam( |
| 648 TypeParameterElement typeParameter) { | 639 TypeParameterElement typeParameter) { |
| 649 UnlinkedTypeParamBuilder b = new UnlinkedTypeParamBuilder(ctx); | 640 UnlinkedTypeParamBuilder b = new UnlinkedTypeParamBuilder(); |
| 650 b.name = typeParameter.name; | 641 b.name = typeParameter.name; |
| 651 b.nameOffset = typeParameter.nameOffset; | 642 b.nameOffset = typeParameter.nameOffset; |
| 652 if (typeParameter.bound != null) { | 643 if (typeParameter.bound != null) { |
| 653 b.bound = serializeTypeRef(typeParameter.bound, typeParameter); | 644 b.bound = serializeTypeRef(typeParameter.bound, typeParameter); |
| 654 } | 645 } |
| 655 return b; | 646 return b; |
| 656 } | 647 } |
| 657 | 648 |
| 658 /** | 649 /** |
| 659 * Serialize the given [type] into an [UnlinkedTypeRef]. | 650 * Serialize the given [type] into an [UnlinkedTypeRef]. |
| 660 */ | 651 */ |
| 661 UnlinkedTypeRefBuilder serializeTypeRef(DartType type, Element context) { | 652 UnlinkedTypeRefBuilder serializeTypeRef(DartType type, Element context) { |
| 662 UnlinkedTypeRefBuilder b = new UnlinkedTypeRefBuilder(ctx); | 653 UnlinkedTypeRefBuilder b = new UnlinkedTypeRefBuilder(); |
| 663 if (type is TypeParameterType) { | 654 if (type is TypeParameterType) { |
| 664 b.paramReference = findTypeParameterIndex(type, context); | 655 b.paramReference = findTypeParameterIndex(type, context); |
| 665 } else { | 656 } else { |
| 666 Element element = type.element; | 657 Element element = type.element; |
| 667 LibraryElement dependentLibrary = element.library; | 658 LibraryElement dependentLibrary = element.library; |
| 668 if (dependentLibrary == null) { | 659 if (dependentLibrary == null) { |
| 669 assert(type.isDynamic); | 660 assert(type.isDynamic); |
| 670 if (type is UndefinedTypeImpl) { | 661 if (type is UndefinedTypeImpl) { |
| 671 b.reference = serializeUnresolvedReference(); | 662 b.reference = serializeUnresolvedReference(); |
| 672 } else { | 663 } else { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 687 // Figure out a prefix that may be used to refer to the given type. | 678 // Figure out a prefix that may be used to refer to the given type. |
| 688 // TODO(paulberry): to avoid subtle relinking inconsistencies we | 679 // TODO(paulberry): to avoid subtle relinking inconsistencies we |
| 689 // should use the actual prefix from the AST (a given type may be | 680 // should use the actual prefix from the AST (a given type may be |
| 690 // reachable via multiple prefixes), but sadly, this information is | 681 // reachable via multiple prefixes), but sadly, this information is |
| 691 // not recorded in the element model. | 682 // not recorded in the element model. |
| 692 int prefixReference = 0; | 683 int prefixReference = 0; |
| 693 PrefixElement prefix = prefixMap[element]; | 684 PrefixElement prefix = prefixMap[element]; |
| 694 if (prefix != null) { | 685 if (prefix != null) { |
| 695 prefixReference = serializePrefix(prefix); | 686 prefixReference = serializePrefix(prefix); |
| 696 } | 687 } |
| 697 unlinkedReferences.add(encodeUnlinkedReference(ctx, | 688 unlinkedReferences.add(encodeUnlinkedReference( |
| 698 name: element.name, prefixReference: prefixReference)); | 689 name: element.name, prefixReference: prefixReference)); |
| 699 prelinkedReferences.add(encodePrelinkedReference(ctx, | 690 prelinkedReferences.add(encodePrelinkedReference( |
| 700 dependency: serializeDependency(dependentLibrary), | 691 dependency: serializeDependency(dependentLibrary), |
| 701 kind: element is FunctionTypeAliasElement | 692 kind: element is FunctionTypeAliasElement |
| 702 ? PrelinkedReferenceKind.typedef | 693 ? PrelinkedReferenceKind.typedef |
| 703 : PrelinkedReferenceKind.classOrEnum, | 694 : PrelinkedReferenceKind.classOrEnum, |
| 704 unit: unit, | 695 unit: unit, |
| 705 numTypeParameters: numTypeParameters)); | 696 numTypeParameters: numTypeParameters)); |
| 706 return index; | 697 return index; |
| 707 }); | 698 }); |
| 708 } | 699 } |
| 709 List<DartType> typeArguments; | 700 List<DartType> typeArguments; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 728 * unresolved references. A new entry is added to the table if necessary to | 719 * unresolved references. A new entry is added to the table if necessary to |
| 729 * satisfy the request. | 720 * satisfy the request. |
| 730 */ | 721 */ |
| 731 int serializeUnresolvedReference() { | 722 int serializeUnresolvedReference() { |
| 732 // TODO(paulberry): in order for relinking to work, we need to record the | 723 // TODO(paulberry): in order for relinking to work, we need to record the |
| 733 // name and prefix of the unresolved symbol. This is not (yet) encoded in | 724 // name and prefix of the unresolved symbol. This is not (yet) encoded in |
| 734 // the element model. | 725 // the element model. |
| 735 if (unresolvedReferenceIndex == null) { | 726 if (unresolvedReferenceIndex == null) { |
| 736 assert(unlinkedReferences.length == prelinkedReferences.length); | 727 assert(unlinkedReferences.length == prelinkedReferences.length); |
| 737 unresolvedReferenceIndex = unlinkedReferences.length; | 728 unresolvedReferenceIndex = unlinkedReferences.length; |
| 738 unlinkedReferences.add(encodeUnlinkedReference(ctx)); | 729 unlinkedReferences.add(encodeUnlinkedReference()); |
| 739 prelinkedReferences.add(encodePrelinkedReference(ctx, | 730 prelinkedReferences.add( |
| 740 kind: PrelinkedReferenceKind.unresolved)); | 731 encodePrelinkedReference(kind: PrelinkedReferenceKind.unresolved)); |
| 741 } | 732 } |
| 742 return unresolvedReferenceIndex; | 733 return unresolvedReferenceIndex; |
| 743 } | 734 } |
| 744 | 735 |
| 745 /** | 736 /** |
| 746 * Serialize the given [variable], creating an [UnlinkedVariable]. | 737 * Serialize the given [variable], creating an [UnlinkedVariable]. |
| 747 */ | 738 */ |
| 748 UnlinkedVariableBuilder serializeVariable(PropertyInducingElement variable) { | 739 UnlinkedVariableBuilder serializeVariable(PropertyInducingElement variable) { |
| 749 UnlinkedVariableBuilder b = new UnlinkedVariableBuilder(ctx); | 740 UnlinkedVariableBuilder b = new UnlinkedVariableBuilder(); |
| 750 b.name = variable.name; | 741 b.name = variable.name; |
| 751 b.nameOffset = variable.nameOffset; | 742 b.nameOffset = variable.nameOffset; |
| 752 b.type = serializeTypeRef(variable.type, variable); | 743 b.type = serializeTypeRef(variable.type, variable); |
| 753 b.isStatic = variable.isStatic && variable.enclosingElement is ClassElement; | 744 b.isStatic = variable.isStatic && variable.enclosingElement is ClassElement; |
| 754 b.isFinal = variable.isFinal; | 745 b.isFinal = variable.isFinal; |
| 755 b.isConst = variable.isConst; | 746 b.isConst = variable.isConst; |
| 756 b.hasImplicitType = variable.hasImplicitType; | 747 b.hasImplicitType = variable.hasImplicitType; |
| 757 b.documentationComment = serializeDocumentation(variable); | 748 b.documentationComment = serializeDocumentation(variable); |
| 758 return b; | 749 return b; |
| 759 } | 750 } |
| 760 } | 751 } |
| OLD | NEW |