| 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/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
| 10 import 'package:analyzer/dart/element/element.dart'; | 10 import 'package:analyzer/dart/element/element.dart'; |
| (...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 /** | 512 /** |
| 513 * Serialize annotations from the given [element]. If [element] has no | 513 * Serialize annotations from the given [element]. If [element] has no |
| 514 * annotations, the empty list is returned. | 514 * annotations, the empty list is returned. |
| 515 */ | 515 */ |
| 516 List<UnlinkedConstBuilder> serializeAnnotations(Element element) { | 516 List<UnlinkedConstBuilder> serializeAnnotations(Element element) { |
| 517 if (element.metadata.isEmpty) { | 517 if (element.metadata.isEmpty) { |
| 518 return const <UnlinkedConstBuilder>[]; | 518 return const <UnlinkedConstBuilder>[]; |
| 519 } | 519 } |
| 520 return element.metadata.map((ElementAnnotation a) { | 520 return element.metadata.map((ElementAnnotation a) { |
| 521 _ConstExprSerializer serializer = | 521 _ConstExprSerializer serializer = |
| 522 new _ConstExprSerializer(this, element, null); | 522 new _ConstExprSerializer(this, element, null, null); |
| 523 serializer | 523 serializer |
| 524 .serializeAnnotation((a as ElementAnnotationImpl).annotationAst); | 524 .serializeAnnotation((a as ElementAnnotationImpl).annotationAst); |
| 525 return serializer.toBuilder(); | 525 return serializer.toBuilder(); |
| 526 }).toList(); | 526 }).toList(); |
| 527 } | 527 } |
| 528 | 528 |
| 529 /** | 529 /** |
| 530 * Return the index of the entry in the references table | 530 * Return the index of the entry in the references table |
| 531 * ([LinkedLibrary.references]) used for the "bottom" type. A new entry is | 531 * ([LinkedLibrary.references]) used for the "bottom" type. A new entry is |
| 532 * added to the table if necessary to satisfy the request. | 532 * added to the table if necessary to satisfy the request. |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 657 b.end = combinator.end; | 657 b.end = combinator.end; |
| 658 } else if (combinator is HideElementCombinator) { | 658 } else if (combinator is HideElementCombinator) { |
| 659 b.hides = combinator.hiddenNames; | 659 b.hides = combinator.hiddenNames; |
| 660 } | 660 } |
| 661 return b; | 661 return b; |
| 662 } | 662 } |
| 663 | 663 |
| 664 /** | 664 /** |
| 665 * Serialize the given [expression], creating an [UnlinkedConstBuilder]. | 665 * Serialize the given [expression], creating an [UnlinkedConstBuilder]. |
| 666 */ | 666 */ |
| 667 UnlinkedConstBuilder serializeConstExpr( | 667 UnlinkedConstBuilder serializeConstExpr(Element context, |
| 668 Element context, Expression expression, | 668 ExecutableElement executableContext, Expression expression, |
| 669 [Set<String> constructorParameterNames]) { | 669 [Set<String> constructorParameterNames]) { |
| 670 _ConstExprSerializer serializer = | 670 _ConstExprSerializer serializer = new _ConstExprSerializer( |
| 671 new _ConstExprSerializer(this, context, constructorParameterNames); | 671 this, context, executableContext, constructorParameterNames); |
| 672 serializer.serialize(expression); | 672 serializer.serialize(expression); |
| 673 return serializer.toBuilder(); | 673 return serializer.toBuilder(); |
| 674 } | 674 } |
| 675 | 675 |
| 676 /** | 676 /** |
| 677 * Serialize documentation from the given [element], creating an | 677 * Serialize documentation from the given [element], creating an |
| 678 * [UnlinkedDocumentationComment]. | 678 * [UnlinkedDocumentationComment]. |
| 679 * | 679 * |
| 680 * If [element] has no documentation, `null` is returned. | 680 * If [element] has no documentation, `null` is returned. |
| 681 */ | 681 */ |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 776 } | 776 } |
| 777 if (executableElement.isConst) { | 777 if (executableElement.isConst) { |
| 778 b.constCycleSlot = storeConstCycle(!executableElement.isCycleFree); | 778 b.constCycleSlot = storeConstCycle(!executableElement.isCycleFree); |
| 779 if (executableElement.constantInitializers != null) { | 779 if (executableElement.constantInitializers != null) { |
| 780 Set<String> constructorParameterNames = | 780 Set<String> constructorParameterNames = |
| 781 executableElement.parameters.map((p) => p.name).toSet(); | 781 executableElement.parameters.map((p) => p.name).toSet(); |
| 782 b.constantInitializers = executableElement.constantInitializers | 782 b.constantInitializers = executableElement.constantInitializers |
| 783 .map((ConstructorInitializer initializer) => | 783 .map((ConstructorInitializer initializer) => |
| 784 serializeConstructorInitializer( | 784 serializeConstructorInitializer( |
| 785 initializer, | 785 initializer, |
| 786 (expr) => serializeConstExpr( | 786 (expr) => serializeConstExpr(executableElement, |
| 787 executableElement, expr, constructorParameterNames))) | 787 executableElement, expr, constructorParameterNames))) |
| 788 .toList(); | 788 .toList(); |
| 789 } | 789 } |
| 790 } | 790 } |
| 791 } else { | 791 } else { |
| 792 b.kind = UnlinkedExecutableKind.functionOrMethod; | 792 b.kind = UnlinkedExecutableKind.functionOrMethod; |
| 793 } | 793 } |
| 794 b.isAbstract = executableElement.isAbstract; | 794 b.isAbstract = executableElement.isAbstract; |
| 795 b.isAsynchronous = executableElement.isAsynchronous; | 795 b.isAsynchronous = executableElement.isAsynchronous; |
| 796 b.isGenerator = executableElement.isGenerator; | 796 b.isGenerator = executableElement.isGenerator; |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 916 .map((parameter) => serializeParam(parameter, context)) | 916 .map((parameter) => serializeParam(parameter, context)) |
| 917 .toList(); | 917 .toList(); |
| 918 } else { | 918 } else { |
| 919 b.type = serializeTypeRef(type, context); | 919 b.type = serializeTypeRef(type, context); |
| 920 } | 920 } |
| 921 } | 921 } |
| 922 if (parameter is ConstVariableElement) { | 922 if (parameter is ConstVariableElement) { |
| 923 ConstVariableElement constParameter = parameter as ConstVariableElement; | 923 ConstVariableElement constParameter = parameter as ConstVariableElement; |
| 924 Expression initializer = constParameter.constantInitializer; | 924 Expression initializer = constParameter.constantInitializer; |
| 925 if (initializer != null) { | 925 if (initializer != null) { |
| 926 b.defaultValue = serializeConstExpr(parameter, initializer); | 926 b.defaultValue = serializeConstExpr( |
| 927 parameter, |
| 928 parameter.getAncestor((Element e) => e is ExecutableElement), |
| 929 initializer); |
| 927 b.defaultValueCode = parameter.defaultValueCode; | 930 b.defaultValueCode = parameter.defaultValueCode; |
| 928 } | 931 } |
| 929 } | 932 } |
| 930 // TODO(scheglov) VariableMember.initializer is not implemented | 933 // TODO(scheglov) VariableMember.initializer is not implemented |
| 931 if (parameter is! VariableMember && parameter.initializer != null) { | 934 if (parameter is! VariableMember && parameter.initializer != null) { |
| 932 b.initializer = serializeExecutable(parameter.initializer); | 935 b.initializer = serializeExecutable(parameter.initializer); |
| 933 } | 936 } |
| 934 { | 937 { |
| 935 SourceRange visibleRange = parameter.visibleRange; | 938 SourceRange visibleRange = parameter.visibleRange; |
| 936 if (visibleRange != null) { | 939 if (visibleRange != null) { |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1127 } | 1130 } |
| 1128 b.isStatic = variable.isStatic && variable.enclosingElement is ClassElement; | 1131 b.isStatic = variable.isStatic && variable.enclosingElement is ClassElement; |
| 1129 b.isFinal = variable.isFinal; | 1132 b.isFinal = variable.isFinal; |
| 1130 b.isConst = variable.isConst; | 1133 b.isConst = variable.isConst; |
| 1131 b.documentationComment = serializeDocumentation(variable); | 1134 b.documentationComment = serializeDocumentation(variable); |
| 1132 b.annotations = serializeAnnotations(variable); | 1135 b.annotations = serializeAnnotations(variable); |
| 1133 if (variable is ConstVariableElement) { | 1136 if (variable is ConstVariableElement) { |
| 1134 ConstVariableElement constVariable = variable as ConstVariableElement; | 1137 ConstVariableElement constVariable = variable as ConstVariableElement; |
| 1135 Expression initializer = constVariable.constantInitializer; | 1138 Expression initializer = constVariable.constantInitializer; |
| 1136 if (initializer != null) { | 1139 if (initializer != null) { |
| 1137 b.constExpr = serializeConstExpr(variable, initializer); | 1140 b.constExpr = |
| 1141 serializeConstExpr(variable, variable.initializer, initializer); |
| 1138 } | 1142 } |
| 1139 } | 1143 } |
| 1140 if (variable is PropertyInducingElement) { | 1144 if (variable is PropertyInducingElement) { |
| 1141 if (b.isFinal || b.isConst) { | 1145 if (b.isFinal || b.isConst) { |
| 1142 b.propagatedTypeSlot = | 1146 b.propagatedTypeSlot = |
| 1143 storeLinkedType(variable.propagatedType, variable); | 1147 storeLinkedType(variable.propagatedType, variable); |
| 1144 } else { | 1148 } else { |
| 1145 // Variable is not propagable. | 1149 // Variable is not propagable. |
| 1146 assert(variable.propagatedType == null); | 1150 assert(variable.propagatedType == null); |
| 1147 } | 1151 } |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1287 } | 1291 } |
| 1288 } | 1292 } |
| 1289 | 1293 |
| 1290 /** | 1294 /** |
| 1291 * Instances of this class keep track of intermediate state during | 1295 * Instances of this class keep track of intermediate state during |
| 1292 * serialization of a single constant [Expression]. | 1296 * serialization of a single constant [Expression]. |
| 1293 */ | 1297 */ |
| 1294 class _ConstExprSerializer extends AbstractConstExprSerializer { | 1298 class _ConstExprSerializer extends AbstractConstExprSerializer { |
| 1295 final _CompilationUnitSerializer serializer; | 1299 final _CompilationUnitSerializer serializer; |
| 1296 final Element context; | 1300 final Element context; |
| 1301 final ExecutableElement executableContext; |
| 1297 | 1302 |
| 1298 /** | 1303 /** |
| 1299 * If a constructor initializer expression is being serialized, the names of | 1304 * If a constructor initializer expression is being serialized, the names of |
| 1300 * the constructor parameters. Otherwise `null`. | 1305 * the constructor parameters. Otherwise `null`. |
| 1301 */ | 1306 */ |
| 1302 final Set<String> constructorParameterNames; | 1307 final Set<String> constructorParameterNames; |
| 1303 | 1308 |
| 1304 _ConstExprSerializer( | 1309 _ConstExprSerializer(this.serializer, this.context, this.executableContext, |
| 1305 this.serializer, this.context, this.constructorParameterNames); | 1310 this.constructorParameterNames); |
| 1306 | 1311 |
| 1307 @override | 1312 @override |
| 1308 bool isConstructorParameterName(String name) { | 1313 bool isConstructorParameterName(String name) { |
| 1309 return constructorParameterNames?.contains(name) ?? false; | 1314 return constructorParameterNames?.contains(name) ?? false; |
| 1310 } | 1315 } |
| 1311 | 1316 |
| 1312 @override | 1317 @override |
| 1313 void serializeAnnotation(Annotation annotation) { | 1318 void serializeAnnotation(Annotation annotation) { |
| 1314 if (annotation.arguments == null) { | 1319 if (annotation.arguments == null) { |
| 1315 assert(annotation.constructorName == null); | 1320 assert(annotation.constructorName == null); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1356 name.staticElement != null | 1361 name.staticElement != null |
| 1357 ? ReferenceKind.constructor | 1362 ? ReferenceKind.constructor |
| 1358 : ReferenceKind.unresolved, | 1363 : ReferenceKind.unresolved, |
| 1359 prefixReference: typeRef.reference, | 1364 prefixReference: typeRef.reference, |
| 1360 unit: typeLinkedRef.unit); | 1365 unit: typeLinkedRef.unit); |
| 1361 return new EntityRefBuilder( | 1366 return new EntityRefBuilder( |
| 1362 reference: refId, typeArguments: typeRef.typeArguments); | 1367 reference: refId, typeArguments: typeRef.typeArguments); |
| 1363 } | 1368 } |
| 1364 } | 1369 } |
| 1365 | 1370 |
| 1371 @override |
| 1372 List<int> serializeFunctionExpression(FunctionExpression functionExpression) { |
| 1373 if (executableContext == null) { |
| 1374 return null; |
| 1375 } |
| 1376 ExecutableElement functionElement = functionExpression.element; |
| 1377 // TOOD(paulberry): handle the situation where [functionExpression] is not |
| 1378 // an immediate child of [executableContext]. |
| 1379 assert(functionElement.enclosingElement == executableContext); |
| 1380 int popCount = 0; |
| 1381 int localIndex = executableContext.functions.indexOf(functionElement); |
| 1382 assert(localIndex != -1); |
| 1383 return <int>[popCount, localIndex]; |
| 1384 } |
| 1385 |
| 1366 EntityRefBuilder serializeIdentifier(Identifier identifier, | 1386 EntityRefBuilder serializeIdentifier(Identifier identifier, |
| 1367 {int prefixReference: 0}) { | 1387 {int prefixReference: 0}) { |
| 1368 if (identifier is SimpleIdentifier) { | 1388 if (identifier is SimpleIdentifier) { |
| 1369 Element element = identifier.staticElement; | 1389 Element element = identifier.staticElement; |
| 1370 if (element is TypeParameterElement) { | 1390 if (element is TypeParameterElement) { |
| 1371 int typeParameterIndex = | 1391 int typeParameterIndex = |
| 1372 serializer.findTypeParameterIndex(element.type, context); | 1392 serializer.findTypeParameterIndex(element.type, context); |
| 1373 return new EntityRefBuilder(paramReference: typeParameterIndex); | 1393 return new EntityRefBuilder(paramReference: typeParameterIndex); |
| 1374 } else if (_isPrelinkResolvableElement(element)) { | 1394 } else if (_isPrelinkResolvableElement(element)) { |
| 1375 int ref = serializer._getElementReferenceId(element); | 1395 int ref = serializer._getElementReferenceId(element); |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1655 exportNames.add(new LinkedExportNameBuilder( | 1675 exportNames.add(new LinkedExportNameBuilder( |
| 1656 name: name, | 1676 name: name, |
| 1657 dependency: serializeDependency(dependentLibrary), | 1677 dependency: serializeDependency(dependentLibrary), |
| 1658 unit: unit, | 1678 unit: unit, |
| 1659 kind: kind)); | 1679 kind: kind)); |
| 1660 } | 1680 } |
| 1661 pb.exportNames = exportNames; | 1681 pb.exportNames = exportNames; |
| 1662 return pb; | 1682 return pb; |
| 1663 } | 1683 } |
| 1664 } | 1684 } |
| OLD | NEW |