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 unlinkedUnit.codeRange = serializeCodeRange(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 b.codeRange = serializeCodeRange(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 CodeRangeBuilder serializeCodeRange(Element element) { |
| 592 if (element is ElementImpl && element.codeOffset != null) { |
| 593 return new CodeRangeBuilder(offset: element.codeOffset, length: element.co
deLength); |
| 594 } |
| 595 return null; |
| 596 } |
| 597 |
589 /** | 598 /** |
590 * Serialize the given [combinator] into an [UnlinkedCombinator]. | 599 * Serialize the given [combinator] into an [UnlinkedCombinator]. |
591 */ | 600 */ |
592 UnlinkedCombinatorBuilder serializeCombinator( | 601 UnlinkedCombinatorBuilder serializeCombinator( |
593 NamespaceCombinator combinator) { | 602 NamespaceCombinator combinator) { |
594 UnlinkedCombinatorBuilder b = new UnlinkedCombinatorBuilder(); | 603 UnlinkedCombinatorBuilder b = new UnlinkedCombinatorBuilder(); |
595 if (combinator is ShowElementCombinator) { | 604 if (combinator is ShowElementCombinator) { |
596 b.shows = combinator.shownNames; | 605 b.shows = combinator.shownNames; |
597 b.offset = combinator.offset; | 606 b.offset = combinator.offset; |
598 b.end = combinator.end; | 607 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) { | 650 if (field.isConst && field.type.element == enumElement) { |
642 values.add(new UnlinkedEnumValueBuilder( | 651 values.add(new UnlinkedEnumValueBuilder( |
643 name: field.name, | 652 name: field.name, |
644 nameOffset: field.nameOffset, | 653 nameOffset: field.nameOffset, |
645 documentationComment: serializeDocumentation(field))); | 654 documentationComment: serializeDocumentation(field))); |
646 } | 655 } |
647 } | 656 } |
648 b.values = values; | 657 b.values = values; |
649 b.documentationComment = serializeDocumentation(enumElement); | 658 b.documentationComment = serializeDocumentation(enumElement); |
650 b.annotations = serializeAnnotations(enumElement); | 659 b.annotations = serializeAnnotations(enumElement); |
| 660 b.codeRange = serializeCodeRange(enumElement); |
651 return b; | 661 return b; |
652 } | 662 } |
653 | 663 |
654 /** | 664 /** |
655 * Serialize the given [executableElement], creating an [UnlinkedExecutable]. | 665 * Serialize the given [executableElement], creating an [UnlinkedExecutable]. |
656 */ | 666 */ |
657 UnlinkedExecutableBuilder serializeExecutable( | 667 UnlinkedExecutableBuilder serializeExecutable( |
658 ExecutableElement executableElement) { | 668 ExecutableElement executableElement) { |
659 UnlinkedExecutableBuilder b = new UnlinkedExecutableBuilder(); | 669 UnlinkedExecutableBuilder b = new UnlinkedExecutableBuilder(); |
660 b.name = executableElement.name; | 670 b.name = executableElement.name; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
729 } | 739 } |
730 } else { | 740 } else { |
731 b.kind = UnlinkedExecutableKind.functionOrMethod; | 741 b.kind = UnlinkedExecutableKind.functionOrMethod; |
732 } | 742 } |
733 b.isAbstract = executableElement.isAbstract; | 743 b.isAbstract = executableElement.isAbstract; |
734 b.isStatic = executableElement.isStatic && | 744 b.isStatic = executableElement.isStatic && |
735 executableElement.enclosingElement is ClassElement; | 745 executableElement.enclosingElement is ClassElement; |
736 b.isExternal = executableElement.isExternal; | 746 b.isExternal = executableElement.isExternal; |
737 b.documentationComment = serializeDocumentation(executableElement); | 747 b.documentationComment = serializeDocumentation(executableElement); |
738 b.annotations = serializeAnnotations(executableElement); | 748 b.annotations = serializeAnnotations(executableElement); |
| 749 b.codeRange = serializeCodeRange(executableElement); |
739 if (executableElement is FunctionElement) { | 750 if (executableElement is FunctionElement) { |
740 SourceRange visibleRange = executableElement.visibleRange; | 751 SourceRange visibleRange = executableElement.visibleRange; |
741 if (visibleRange != null) { | 752 if (visibleRange != null) { |
742 b.visibleOffset = visibleRange.offset; | 753 b.visibleOffset = visibleRange.offset; |
743 b.visibleLength = visibleRange.length; | 754 b.visibleLength = visibleRange.length; |
744 } | 755 } |
745 } | 756 } |
746 b.localFunctions = | 757 b.localFunctions = |
747 executableElement.functions.map(serializeExecutable).toList(); | 758 executableElement.functions.map(serializeExecutable).toList(); |
748 b.localLabels = executableElement.labels.map(serializeLabel).toList(); | 759 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; | 836 b.kind = UnlinkedParamKind.required; |
826 break; | 837 break; |
827 case ParameterKind.POSITIONAL: | 838 case ParameterKind.POSITIONAL: |
828 b.kind = UnlinkedParamKind.positional; | 839 b.kind = UnlinkedParamKind.positional; |
829 break; | 840 break; |
830 case ParameterKind.NAMED: | 841 case ParameterKind.NAMED: |
831 b.kind = UnlinkedParamKind.named; | 842 b.kind = UnlinkedParamKind.named; |
832 break; | 843 break; |
833 } | 844 } |
834 b.annotations = serializeAnnotations(parameter); | 845 b.annotations = serializeAnnotations(parameter); |
| 846 b.codeRange = serializeCodeRange(parameter); |
835 b.isInitializingFormal = parameter.isInitializingFormal; | 847 b.isInitializingFormal = parameter.isInitializingFormal; |
836 DartType type = parameter.type; | 848 DartType type = parameter.type; |
837 if (parameter.hasImplicitType) { | 849 if (parameter.hasImplicitType) { |
838 Element contextParent = context.enclosingElement; | 850 Element contextParent = context.enclosingElement; |
839 if (!parameter.isInitializingFormal && | 851 if (!parameter.isInitializingFormal && |
840 contextParent is ExecutableElement && | 852 contextParent is ExecutableElement && |
841 !contextParent.isStatic && | 853 !contextParent.isStatic && |
842 contextParent is! ConstructorElement) { | 854 contextParent is! ConstructorElement) { |
843 b.inferredTypeSlot = storeInferredType(type, context); | 855 b.inferredTypeSlot = storeInferredType(type, context); |
844 } | 856 } |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
914 FunctionTypeAliasElement typedefElement) { | 926 FunctionTypeAliasElement typedefElement) { |
915 UnlinkedTypedefBuilder b = new UnlinkedTypedefBuilder(); | 927 UnlinkedTypedefBuilder b = new UnlinkedTypedefBuilder(); |
916 b.name = typedefElement.name; | 928 b.name = typedefElement.name; |
917 b.nameOffset = typedefElement.nameOffset; | 929 b.nameOffset = typedefElement.nameOffset; |
918 b.typeParameters = | 930 b.typeParameters = |
919 typedefElement.typeParameters.map(serializeTypeParam).toList(); | 931 typedefElement.typeParameters.map(serializeTypeParam).toList(); |
920 b.returnType = serializeTypeRef(typedefElement.returnType, typedefElement); | 932 b.returnType = serializeTypeRef(typedefElement.returnType, typedefElement); |
921 b.parameters = typedefElement.parameters.map(serializeParam).toList(); | 933 b.parameters = typedefElement.parameters.map(serializeParam).toList(); |
922 b.documentationComment = serializeDocumentation(typedefElement); | 934 b.documentationComment = serializeDocumentation(typedefElement); |
923 b.annotations = serializeAnnotations(typedefElement); | 935 b.annotations = serializeAnnotations(typedefElement); |
| 936 b.codeRange = serializeCodeRange(typedefElement); |
924 return b; | 937 return b; |
925 } | 938 } |
926 | 939 |
927 /** | 940 /** |
928 * Serialize the given [typeParameter] into an [UnlinkedTypeParam]. | 941 * Serialize the given [typeParameter] into an [UnlinkedTypeParam]. |
929 */ | 942 */ |
930 UnlinkedTypeParamBuilder serializeTypeParam( | 943 UnlinkedTypeParamBuilder serializeTypeParam( |
931 TypeParameterElement typeParameter) { | 944 TypeParameterElement typeParameter) { |
932 UnlinkedTypeParamBuilder b = new UnlinkedTypeParamBuilder(); | 945 UnlinkedTypeParamBuilder b = new UnlinkedTypeParamBuilder(); |
933 b.name = typeParameter.name; | 946 b.name = typeParameter.name; |
934 b.nameOffset = typeParameter.nameOffset; | 947 b.nameOffset = typeParameter.nameOffset; |
935 if (typeParameter.bound != null) { | 948 if (typeParameter.bound != null) { |
936 b.bound = serializeTypeRef(typeParameter.bound, typeParameter); | 949 b.bound = serializeTypeRef(typeParameter.bound, typeParameter); |
937 } | 950 } |
938 b.annotations = serializeAnnotations(typeParameter); | 951 b.annotations = serializeAnnotations(typeParameter); |
| 952 b.codeRange = serializeCodeRange(typeParameter); |
939 return b; | 953 return b; |
940 } | 954 } |
941 | 955 |
942 /** | 956 /** |
943 * Serialize the given [type] into a [EntityRef]. If [slot] is provided, | 957 * Serialize the given [type] into a [EntityRef]. If [slot] is provided, |
944 * it should be included in the [EntityRef]. | 958 * it should be included in the [EntityRef]. |
945 * | 959 * |
946 * [context] is the element within which the [EntityRef] will be | 960 * [context] is the element within which the [EntityRef] will be |
947 * interpreted; this is used to serialize type parameters. | 961 * interpreted; this is used to serialize type parameters. |
948 */ | 962 */ |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1078 storeLinkedType(variable.propagatedType, variable); | 1092 storeLinkedType(variable.propagatedType, variable); |
1079 } else { | 1093 } else { |
1080 // Variable is not propagable. | 1094 // Variable is not propagable. |
1081 assert(variable.propagatedType == null); | 1095 assert(variable.propagatedType == null); |
1082 } | 1096 } |
1083 } | 1097 } |
1084 if (variable.hasImplicitType && | 1098 if (variable.hasImplicitType && |
1085 (variable.initializer != null || !variable.isStatic)) { | 1099 (variable.initializer != null || !variable.isStatic)) { |
1086 b.inferredTypeSlot = storeInferredType(variable.type, variable); | 1100 b.inferredTypeSlot = storeInferredType(variable.type, variable); |
1087 } | 1101 } |
| 1102 b.codeRange = serializeCodeRange(variable); |
1088 if (variable is LocalVariableElement) { | 1103 if (variable is LocalVariableElement) { |
1089 SourceRange visibleRange = variable.visibleRange; | 1104 SourceRange visibleRange = variable.visibleRange; |
1090 if (visibleRange != null) { | 1105 if (visibleRange != null) { |
1091 b.visibleOffset = visibleRange.offset; | 1106 b.visibleOffset = visibleRange.offset; |
1092 b.visibleLength = visibleRange.length; | 1107 b.visibleLength = visibleRange.length; |
1093 } | 1108 } |
1094 } | 1109 } |
1095 // TODO(scheglov) VariableMember.initializer is not implemented | 1110 // TODO(scheglov) VariableMember.initializer is not implemented |
1096 if (variable is! VariableMember && variable.initializer != null) { | 1111 if (variable is! VariableMember && variable.initializer != null) { |
1097 b.initializer = serializeExecutable(variable.initializer); | 1112 b.initializer = serializeExecutable(variable.initializer); |
1098 } | 1113 } |
1099 return b; | 1114 return b; |
1100 } | 1115 } |
1101 | 1116 |
1102 /** | 1117 /** |
| 1118 * Create a new slot id and return it. If [hasCycle] is `true`, arrange for |
| 1119 * the slot id to be included in [LinkedUnit.constCycles]. |
| 1120 */ |
| 1121 int storeConstCycle(bool hasCycle) { |
| 1122 int slot = ++numSlots; |
| 1123 if (hasCycle) { |
| 1124 constCycles.add(slot); |
| 1125 } |
| 1126 return slot; |
| 1127 } |
| 1128 |
| 1129 /** |
1103 * Create a slot id for the given [type] (which is an inferred type). If | 1130 * 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 | 1131 * [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 | 1132 * compilation unit has been fully visited, it will be serialized into |
1106 * [LinkedUnit.types]. | 1133 * [LinkedUnit.types]. |
1107 * | 1134 * |
1108 * [context] is the element within which the slot id will appear; this is | 1135 * [context] is the element within which the slot id will appear; this is |
1109 * used to serialize type parameters. | 1136 * used to serialize type parameters. |
1110 */ | 1137 */ |
1111 int storeInferredType(DartType type, Element context) { | 1138 int storeInferredType(DartType type, Element context) { |
1112 return storeLinkedType(type.isDynamic ? null : type, context); | 1139 return storeLinkedType(type.isDynamic ? null : type, context); |
1113 } | 1140 } |
1114 | 1141 |
1115 /** | 1142 /** |
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 | 1143 * 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 | 1144 * 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, | 1145 * [linkedTypes] so that once the compilation unit has been fully visited, |
1131 * it will be serialized to [LinkedUnit.types]. | 1146 * it will be serialized to [LinkedUnit.types]. |
1132 * | 1147 * |
1133 * [context] is the element within which the slot id will appear; this is | 1148 * [context] is the element within which the slot id will appear; this is |
1134 * used to serialize type parameters. | 1149 * used to serialize type parameters. |
1135 */ | 1150 */ |
1136 int storeLinkedType(DartType type, Element context) { | 1151 int storeLinkedType(DartType type, Element context) { |
1137 int slot = ++numSlots; | 1152 int slot = ++numSlots; |
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1564 exportNames.add(new LinkedExportNameBuilder( | 1579 exportNames.add(new LinkedExportNameBuilder( |
1565 name: name, | 1580 name: name, |
1566 dependency: serializeDependency(dependentLibrary), | 1581 dependency: serializeDependency(dependentLibrary), |
1567 unit: unit, | 1582 unit: unit, |
1568 kind: kind)); | 1583 kind: kind)); |
1569 } | 1584 } |
1570 pb.exportNames = exportNames; | 1585 pb.exportNames = exportNames; |
1571 return pb; | 1586 return pb; |
1572 } | 1587 } |
1573 } | 1588 } |
OLD | NEW |