OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library serialization.elements; | 5 library serialization.elements; |
6 | 6 |
7 import 'dart:convert'; | 7 import 'dart:convert'; |
8 | 8 |
9 import 'package:analyzer/dart/element/element.dart'; | 9 import 'package:analyzer/dart/element/element.dart'; |
10 import 'package:analyzer/dart/element/type.dart'; | 10 import 'package:analyzer/dart/element/type.dart'; |
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 } else { | 362 } else { |
363 // TODO(paulberry): we need to figure out a way to record library, part, | 363 // TODO(paulberry): we need to figure out a way to record library, part, |
364 // import, and export declarations that appear in non-defining | 364 // import, and export declarations that appear in non-defining |
365 // compilation units (even though such declarations are prohibited by the | 365 // compilation units (even though such declarations are prohibited by the |
366 // language), so that if the user makes code changes that cause a | 366 // language), so that if the user makes code changes that cause a |
367 // non-defining compilation unit to become a defining compilation unit, | 367 // non-defining compilation unit to become a defining compilation unit, |
368 // we can create a correct summary by simply re-linking. | 368 // we can create a correct summary by simply re-linking. |
369 unlinkedUnit.publicNamespace = | 369 unlinkedUnit.publicNamespace = |
370 new UnlinkedPublicNamespaceBuilder(names: names); | 370 new UnlinkedPublicNamespaceBuilder(names: names); |
371 } | 371 } |
| 372 serializeCodeRange(unlinkedUnit, compilationUnit); |
372 unlinkedUnit.classes = compilationUnit.types.map(serializeClass).toList(); | 373 unlinkedUnit.classes = compilationUnit.types.map(serializeClass).toList(); |
373 unlinkedUnit.enums = compilationUnit.enums.map(serializeEnum).toList(); | 374 unlinkedUnit.enums = compilationUnit.enums.map(serializeEnum).toList(); |
374 unlinkedUnit.typedefs = | 375 unlinkedUnit.typedefs = |
375 compilationUnit.functionTypeAliases.map(serializeTypedef).toList(); | 376 compilationUnit.functionTypeAliases.map(serializeTypedef).toList(); |
376 List<UnlinkedExecutableBuilder> executables = | 377 List<UnlinkedExecutableBuilder> executables = |
377 compilationUnit.functions.map(serializeExecutable).toList(); | 378 compilationUnit.functions.map(serializeExecutable).toList(); |
378 for (PropertyAccessorElement accessor in compilationUnit.accessors) { | 379 for (PropertyAccessorElement accessor in compilationUnit.accessors) { |
379 if (!accessor.isSynthetic) { | 380 if (!accessor.isSynthetic) { |
380 executables.add(serializeExecutable(accessor)); | 381 executables.add(serializeExecutable(accessor)); |
381 } | 382 } |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
530 fields.add(serializeVariable(field)); | 531 fields.add(serializeVariable(field)); |
531 } | 532 } |
532 } | 533 } |
533 } | 534 } |
534 b.fields = fields; | 535 b.fields = fields; |
535 b.executables = executables; | 536 b.executables = executables; |
536 b.isAbstract = classElement.isAbstract; | 537 b.isAbstract = classElement.isAbstract; |
537 b.isMixinApplication = classElement.isMixinApplication; | 538 b.isMixinApplication = classElement.isMixinApplication; |
538 b.documentationComment = serializeDocumentation(classElement); | 539 b.documentationComment = serializeDocumentation(classElement); |
539 b.annotations = serializeAnnotations(classElement); | 540 b.annotations = serializeAnnotations(classElement); |
| 541 serializeCodeRange(b, classElement); |
540 return b; | 542 return b; |
541 } | 543 } |
542 | 544 |
543 /** | 545 /** |
544 * If [cls] is a class, return the list of its members available for | 546 * If [cls] is a class, return the list of its members available for |
545 * constants - static constant fields, static methods and constructors. | 547 * constants - static constant fields, static methods and constructors. |
546 * Otherwise return `null`. | 548 * Otherwise return `null`. |
547 */ | 549 */ |
548 List<UnlinkedPublicNameBuilder> serializeClassConstMembers(ClassElement cls) { | 550 List<UnlinkedPublicNameBuilder> serializeClassConstMembers(ClassElement cls) { |
549 if (cls.isMixinApplication) { | 551 if (cls.isMixinApplication) { |
(...skipping 29 matching lines...) Expand all Loading... |
579 name: constructor.name, | 581 name: constructor.name, |
580 kind: ReferenceKind.constructor, | 582 kind: ReferenceKind.constructor, |
581 numTypeParameters: 0)); | 583 numTypeParameters: 0)); |
582 } | 584 } |
583 } | 585 } |
584 return bs; | 586 return bs; |
585 } | 587 } |
586 return null; | 588 return null; |
587 } | 589 } |
588 | 590 |
| 591 void serializeCodeRange(Object b, Element element) { |
| 592 if (element is ElementImpl && element.codeOffset != null) { |
| 593 if (b is UnlinkedClassBuilder) { |
| 594 b.hasCodeRange = true; |
| 595 b.codeOffset = element.codeOffset; |
| 596 b.codeLength = element.codeLength; |
| 597 } else if (b is UnlinkedEnumBuilder) { |
| 598 b.hasCodeRange = true; |
| 599 b.codeOffset = element.codeOffset; |
| 600 b.codeLength = element.codeLength; |
| 601 } else if (b is UnlinkedExecutableBuilder) { |
| 602 b.hasCodeRange = true; |
| 603 b.codeOffset = element.codeOffset; |
| 604 b.codeLength = element.codeLength; |
| 605 } else if (b is UnlinkedParamBuilder) { |
| 606 b.hasCodeRange = true; |
| 607 b.codeOffset = element.codeOffset; |
| 608 b.codeLength = element.codeLength; |
| 609 } else if (b is UnlinkedTypedefBuilder) { |
| 610 b.hasCodeRange = true; |
| 611 b.codeOffset = element.codeOffset; |
| 612 b.codeLength = element.codeLength; |
| 613 } else if (b is UnlinkedTypeParamBuilder) { |
| 614 b.hasCodeRange = true; |
| 615 b.codeOffset = element.codeOffset; |
| 616 b.codeLength = element.codeLength; |
| 617 } else if (b is UnlinkedUnitBuilder) { |
| 618 b.hasCodeRange = true; |
| 619 b.codeOffset = element.codeOffset; |
| 620 b.codeLength = element.codeLength; |
| 621 } else if (b is UnlinkedVariableBuilder) { |
| 622 b.hasCodeRange = true; |
| 623 b.codeOffset = element.codeOffset; |
| 624 b.codeLength = element.codeLength; |
| 625 } |
| 626 } |
| 627 } |
| 628 |
589 /** | 629 /** |
590 * Serialize the given [combinator] into an [UnlinkedCombinator]. | 630 * Serialize the given [combinator] into an [UnlinkedCombinator]. |
591 */ | 631 */ |
592 UnlinkedCombinatorBuilder serializeCombinator( | 632 UnlinkedCombinatorBuilder serializeCombinator( |
593 NamespaceCombinator combinator) { | 633 NamespaceCombinator combinator) { |
594 UnlinkedCombinatorBuilder b = new UnlinkedCombinatorBuilder(); | 634 UnlinkedCombinatorBuilder b = new UnlinkedCombinatorBuilder(); |
595 if (combinator is ShowElementCombinator) { | 635 if (combinator is ShowElementCombinator) { |
596 b.shows = combinator.shownNames; | 636 b.shows = combinator.shownNames; |
597 b.offset = combinator.offset; | 637 b.offset = combinator.offset; |
598 b.end = combinator.end; | 638 b.end = combinator.end; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
641 if (field.isConst && field.type.element == enumElement) { | 681 if (field.isConst && field.type.element == enumElement) { |
642 values.add(new UnlinkedEnumValueBuilder( | 682 values.add(new UnlinkedEnumValueBuilder( |
643 name: field.name, | 683 name: field.name, |
644 nameOffset: field.nameOffset, | 684 nameOffset: field.nameOffset, |
645 documentationComment: serializeDocumentation(field))); | 685 documentationComment: serializeDocumentation(field))); |
646 } | 686 } |
647 } | 687 } |
648 b.values = values; | 688 b.values = values; |
649 b.documentationComment = serializeDocumentation(enumElement); | 689 b.documentationComment = serializeDocumentation(enumElement); |
650 b.annotations = serializeAnnotations(enumElement); | 690 b.annotations = serializeAnnotations(enumElement); |
| 691 serializeCodeRange(b, enumElement); |
651 return b; | 692 return b; |
652 } | 693 } |
653 | 694 |
654 /** | 695 /** |
655 * Serialize the given [executableElement], creating an [UnlinkedExecutable]. | 696 * Serialize the given [executableElement], creating an [UnlinkedExecutable]. |
656 */ | 697 */ |
657 UnlinkedExecutableBuilder serializeExecutable( | 698 UnlinkedExecutableBuilder serializeExecutable( |
658 ExecutableElement executableElement) { | 699 ExecutableElement executableElement) { |
659 UnlinkedExecutableBuilder b = new UnlinkedExecutableBuilder(); | 700 UnlinkedExecutableBuilder b = new UnlinkedExecutableBuilder(); |
660 b.name = executableElement.name; | 701 b.name = executableElement.name; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
729 } | 770 } |
730 } else { | 771 } else { |
731 b.kind = UnlinkedExecutableKind.functionOrMethod; | 772 b.kind = UnlinkedExecutableKind.functionOrMethod; |
732 } | 773 } |
733 b.isAbstract = executableElement.isAbstract; | 774 b.isAbstract = executableElement.isAbstract; |
734 b.isStatic = executableElement.isStatic && | 775 b.isStatic = executableElement.isStatic && |
735 executableElement.enclosingElement is ClassElement; | 776 executableElement.enclosingElement is ClassElement; |
736 b.isExternal = executableElement.isExternal; | 777 b.isExternal = executableElement.isExternal; |
737 b.documentationComment = serializeDocumentation(executableElement); | 778 b.documentationComment = serializeDocumentation(executableElement); |
738 b.annotations = serializeAnnotations(executableElement); | 779 b.annotations = serializeAnnotations(executableElement); |
| 780 serializeCodeRange(b, executableElement); |
739 if (executableElement is FunctionElement) { | 781 if (executableElement is FunctionElement) { |
740 SourceRange visibleRange = executableElement.visibleRange; | 782 SourceRange visibleRange = executableElement.visibleRange; |
741 if (visibleRange != null) { | 783 if (visibleRange != null) { |
742 b.visibleOffset = visibleRange.offset; | 784 b.visibleOffset = visibleRange.offset; |
743 b.visibleLength = visibleRange.length; | 785 b.visibleLength = visibleRange.length; |
744 } | 786 } |
745 } | 787 } |
746 b.localFunctions = | 788 b.localFunctions = |
747 executableElement.functions.map(serializeExecutable).toList(); | 789 executableElement.functions.map(serializeExecutable).toList(); |
748 b.localLabels = executableElement.labels.map(serializeLabel).toList(); | 790 b.localLabels = executableElement.labels.map(serializeLabel).toList(); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
825 b.kind = UnlinkedParamKind.required; | 867 b.kind = UnlinkedParamKind.required; |
826 break; | 868 break; |
827 case ParameterKind.POSITIONAL: | 869 case ParameterKind.POSITIONAL: |
828 b.kind = UnlinkedParamKind.positional; | 870 b.kind = UnlinkedParamKind.positional; |
829 break; | 871 break; |
830 case ParameterKind.NAMED: | 872 case ParameterKind.NAMED: |
831 b.kind = UnlinkedParamKind.named; | 873 b.kind = UnlinkedParamKind.named; |
832 break; | 874 break; |
833 } | 875 } |
834 b.annotations = serializeAnnotations(parameter); | 876 b.annotations = serializeAnnotations(parameter); |
| 877 serializeCodeRange(b, parameter); |
835 b.isInitializingFormal = parameter.isInitializingFormal; | 878 b.isInitializingFormal = parameter.isInitializingFormal; |
836 DartType type = parameter.type; | 879 DartType type = parameter.type; |
837 if (parameter.hasImplicitType) { | 880 if (parameter.hasImplicitType) { |
838 Element contextParent = context.enclosingElement; | 881 Element contextParent = context.enclosingElement; |
839 if (!parameter.isInitializingFormal && | 882 if (!parameter.isInitializingFormal && |
840 contextParent is ExecutableElement && | 883 contextParent is ExecutableElement && |
841 !contextParent.isStatic && | 884 !contextParent.isStatic && |
842 contextParent is! ConstructorElement) { | 885 contextParent is! ConstructorElement) { |
843 b.inferredTypeSlot = storeInferredType(type, context); | 886 b.inferredTypeSlot = storeInferredType(type, context); |
844 } | 887 } |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
914 FunctionTypeAliasElement typedefElement) { | 957 FunctionTypeAliasElement typedefElement) { |
915 UnlinkedTypedefBuilder b = new UnlinkedTypedefBuilder(); | 958 UnlinkedTypedefBuilder b = new UnlinkedTypedefBuilder(); |
916 b.name = typedefElement.name; | 959 b.name = typedefElement.name; |
917 b.nameOffset = typedefElement.nameOffset; | 960 b.nameOffset = typedefElement.nameOffset; |
918 b.typeParameters = | 961 b.typeParameters = |
919 typedefElement.typeParameters.map(serializeTypeParam).toList(); | 962 typedefElement.typeParameters.map(serializeTypeParam).toList(); |
920 b.returnType = serializeTypeRef(typedefElement.returnType, typedefElement); | 963 b.returnType = serializeTypeRef(typedefElement.returnType, typedefElement); |
921 b.parameters = typedefElement.parameters.map(serializeParam).toList(); | 964 b.parameters = typedefElement.parameters.map(serializeParam).toList(); |
922 b.documentationComment = serializeDocumentation(typedefElement); | 965 b.documentationComment = serializeDocumentation(typedefElement); |
923 b.annotations = serializeAnnotations(typedefElement); | 966 b.annotations = serializeAnnotations(typedefElement); |
| 967 serializeCodeRange(b, typedefElement); |
924 return b; | 968 return b; |
925 } | 969 } |
926 | 970 |
927 /** | 971 /** |
928 * Serialize the given [typeParameter] into an [UnlinkedTypeParam]. | 972 * Serialize the given [typeParameter] into an [UnlinkedTypeParam]. |
929 */ | 973 */ |
930 UnlinkedTypeParamBuilder serializeTypeParam( | 974 UnlinkedTypeParamBuilder serializeTypeParam( |
931 TypeParameterElement typeParameter) { | 975 TypeParameterElement typeParameter) { |
932 UnlinkedTypeParamBuilder b = new UnlinkedTypeParamBuilder(); | 976 UnlinkedTypeParamBuilder b = new UnlinkedTypeParamBuilder(); |
933 b.name = typeParameter.name; | 977 b.name = typeParameter.name; |
934 b.nameOffset = typeParameter.nameOffset; | 978 b.nameOffset = typeParameter.nameOffset; |
935 if (typeParameter.bound != null) { | 979 if (typeParameter.bound != null) { |
936 b.bound = serializeTypeRef(typeParameter.bound, typeParameter); | 980 b.bound = serializeTypeRef(typeParameter.bound, typeParameter); |
937 } | 981 } |
938 b.annotations = serializeAnnotations(typeParameter); | 982 b.annotations = serializeAnnotations(typeParameter); |
| 983 serializeCodeRange(b, typeParameter); |
939 return b; | 984 return b; |
940 } | 985 } |
941 | 986 |
942 /** | 987 /** |
943 * Serialize the given [type] into a [EntityRef]. If [slot] is provided, | 988 * Serialize the given [type] into a [EntityRef]. If [slot] is provided, |
944 * it should be included in the [EntityRef]. | 989 * it should be included in the [EntityRef]. |
945 * | 990 * |
946 * [context] is the element within which the [EntityRef] will be | 991 * [context] is the element within which the [EntityRef] will be |
947 * interpreted; this is used to serialize type parameters. | 992 * interpreted; this is used to serialize type parameters. |
948 */ | 993 */ |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1078 storeLinkedType(variable.propagatedType, variable); | 1123 storeLinkedType(variable.propagatedType, variable); |
1079 } else { | 1124 } else { |
1080 // Variable is not propagable. | 1125 // Variable is not propagable. |
1081 assert(variable.propagatedType == null); | 1126 assert(variable.propagatedType == null); |
1082 } | 1127 } |
1083 } | 1128 } |
1084 if (variable.hasImplicitType && | 1129 if (variable.hasImplicitType && |
1085 (variable.initializer != null || !variable.isStatic)) { | 1130 (variable.initializer != null || !variable.isStatic)) { |
1086 b.inferredTypeSlot = storeInferredType(variable.type, variable); | 1131 b.inferredTypeSlot = storeInferredType(variable.type, variable); |
1087 } | 1132 } |
| 1133 serializeCodeRange(b, variable); |
1088 if (variable is LocalVariableElement) { | 1134 if (variable is LocalVariableElement) { |
1089 SourceRange visibleRange = variable.visibleRange; | 1135 SourceRange visibleRange = variable.visibleRange; |
1090 if (visibleRange != null) { | 1136 if (visibleRange != null) { |
1091 b.visibleOffset = visibleRange.offset; | 1137 b.visibleOffset = visibleRange.offset; |
1092 b.visibleLength = visibleRange.length; | 1138 b.visibleLength = visibleRange.length; |
1093 } | 1139 } |
1094 } | 1140 } |
1095 // TODO(scheglov) VariableMember.initializer is not implemented | 1141 // TODO(scheglov) VariableMember.initializer is not implemented |
1096 if (variable is! VariableMember && variable.initializer != null) { | 1142 if (variable is! VariableMember && variable.initializer != null) { |
1097 b.initializer = serializeExecutable(variable.initializer); | 1143 b.initializer = serializeExecutable(variable.initializer); |
1098 } | 1144 } |
1099 return b; | 1145 return b; |
1100 } | 1146 } |
1101 | 1147 |
1102 /** | 1148 /** |
| 1149 * Create a new slot id and return it. If [hasCycle] is `true`, arrange for |
| 1150 * the slot id to be included in [LinkedUnit.constCycles]. |
| 1151 */ |
| 1152 int storeConstCycle(bool hasCycle) { |
| 1153 int slot = ++numSlots; |
| 1154 if (hasCycle) { |
| 1155 constCycles.add(slot); |
| 1156 } |
| 1157 return slot; |
| 1158 } |
| 1159 |
| 1160 /** |
1103 * Create a slot id for the given [type] (which is an inferred type). If | 1161 * Create a slot id for the given [type] (which is an inferred type). If |
1104 * [type] is not `dynamic`, it is stored in [linkedTypes] so that once the | 1162 * [type] is not `dynamic`, it is stored in [linkedTypes] so that once the |
1105 * compilation unit has been fully visited, it will be serialized into | 1163 * compilation unit has been fully visited, it will be serialized into |
1106 * [LinkedUnit.types]. | 1164 * [LinkedUnit.types]. |
1107 * | 1165 * |
1108 * [context] is the element within which the slot id will appear; this is | 1166 * [context] is the element within which the slot id will appear; this is |
1109 * used to serialize type parameters. | 1167 * used to serialize type parameters. |
1110 */ | 1168 */ |
1111 int storeInferredType(DartType type, Element context) { | 1169 int storeInferredType(DartType type, Element context) { |
1112 return storeLinkedType(type.isDynamic ? null : type, context); | 1170 return storeLinkedType(type.isDynamic ? null : type, context); |
1113 } | 1171 } |
1114 | 1172 |
1115 /** | 1173 /** |
1116 * Create a new slot id and return it. If [hasCycle] is `true`, arrange for | |
1117 * the slot id to be included in [LinkedUnit.constCycles]. | |
1118 */ | |
1119 int storeConstCycle(bool hasCycle) { | |
1120 int slot = ++numSlots; | |
1121 if (hasCycle) { | |
1122 constCycles.add(slot); | |
1123 } | |
1124 return slot; | |
1125 } | |
1126 | |
1127 /** | |
1128 * Create a slot id for the given [type] (which may be either a propagated | 1174 * Create a slot id for the given [type] (which may be either a propagated |
1129 * type or an inferred type). If [type] is not `null`, it is stored in | 1175 * type or an inferred type). If [type] is not `null`, it is stored in |
1130 * [linkedTypes] so that once the compilation unit has been fully visited, | 1176 * [linkedTypes] so that once the compilation unit has been fully visited, |
1131 * it will be serialized to [LinkedUnit.types]. | 1177 * it will be serialized to [LinkedUnit.types]. |
1132 * | 1178 * |
1133 * [context] is the element within which the slot id will appear; this is | 1179 * [context] is the element within which the slot id will appear; this is |
1134 * used to serialize type parameters. | 1180 * used to serialize type parameters. |
1135 */ | 1181 */ |
1136 int storeLinkedType(DartType type, Element context) { | 1182 int storeLinkedType(DartType type, Element context) { |
1137 int slot = ++numSlots; | 1183 int slot = ++numSlots; |
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1564 exportNames.add(new LinkedExportNameBuilder( | 1610 exportNames.add(new LinkedExportNameBuilder( |
1565 name: name, | 1611 name: name, |
1566 dependency: serializeDependency(dependentLibrary), | 1612 dependency: serializeDependency(dependentLibrary), |
1567 unit: unit, | 1613 unit: unit, |
1568 kind: kind)); | 1614 kind: kind)); |
1569 } | 1615 } |
1570 pb.exportNames = exportNames; | 1616 pb.exportNames = exportNames; |
1571 return pb; | 1617 return pb; |
1572 } | 1618 } |
1573 } | 1619 } |
OLD | NEW |