Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(434)

Side by Side Diff: pkg/analyzer/tool/summary/generate.dart

Issue 2216873003: Add an "api signature" to summaries. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 /** 5 /**
6 * This file contains code to generate serialization/deserialization logic for 6 * This file contains code to generate serialization/deserialization logic for
7 * summaries based on an "IDL" description of the summary format (written in 7 * summaries based on an "IDL" description of the summary format (written in
8 * stylized Dart). 8 * stylized Dart).
9 * 9 *
10 * For each class in the "IDL" input, two corresponding classes are generated: 10 * For each class in the "IDL" input, two corresponding classes are generated:
(...skipping 600 matching lines...) Expand 10 before | Expand all | Expand 10 after
611 if (fieldType.isList) { 611 if (fieldType.isList) {
612 out('$valueName?.forEach((b) => b.flushInformative());'); 612 out('$valueName?.forEach((b) => b.flushInformative());');
613 } else { 613 } else {
614 out('$valueName?.flushInformative();'); 614 out('$valueName?.flushInformative();');
615 } 615 }
616 } 616 }
617 } 617 }
618 }); 618 });
619 out('}'); 619 out('}');
620 } 620 }
621 // Generate collectApiSignature().
622 {
623 out();
624 out('/**');
625 out(' * Accumulate non-[informative] data into [signature].');
626 out(' */');
627 out('void collectApiSignature(fb.ApiSignature signature) {');
628 indent(() {
629 List<idlModel.FieldDeclaration> sortedFields = cls.fields.toList()
630 ..sort((idlModel.FieldDeclaration a, idlModel.FieldDeclaration b) =>
631 a.id.compareTo(b.id));
632 for (idlModel.FieldDeclaration field in sortedFields) {
633 if (field.isInformative) {
634 continue;
635 }
636 String ref = 'this._${field.name}';
637 if (field.type.isList) {
638 out('if ($ref == null) {');
639 indent(() {
640 out('signature.addInt(0);');
641 });
642 out('} else {');
643 indent(() {
644 out('signature.addInt($ref.length);');
645 out('for (var x in $ref) {');
646 indent(() {
647 _generateSignatureCall(field.type.typeName, 'x', false);
648 });
649 out('}');
650 });
651 out('}');
652 } else {
653 _generateSignatureCall(field.type.typeName, ref, true);
654 }
655 }
656 });
657 out('}');
658 }
621 // Generate finish. 659 // Generate finish.
622 if (cls.isTopLevel) { 660 if (cls.isTopLevel) {
623 out(); 661 out();
624 out('List<int> toBuffer() {'); 662 out('List<int> toBuffer() {');
625 indent(() { 663 indent(() {
626 out('fb.Builder fbBuilder = new fb.Builder();'); 664 out('fb.Builder fbBuilder = new fb.Builder();');
627 String fileId = cls.fileIdentifier == null 665 String fileId = cls.fileIdentifier == null
628 ? '' 666 ? ''
629 : ', ${quoted(cls.fileIdentifier)}'; 667 : ', ${quoted(cls.fileIdentifier)}';
630 out('return fbBuilder.finish(finish(fbBuilder)$fileId);'); 668 out('return fbBuilder.finish(finish(fbBuilder)$fileId);');
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
813 assert(readCode != null); 851 assert(readCode != null);
814 // Write the getter implementation. 852 // Write the getter implementation.
815 out(); 853 out();
816 out('@override'); 854 out('@override');
817 String returnType = dartType(type); 855 String returnType = dartType(type);
818 if (field.isDeprecated) { 856 if (field.isDeprecated) {
819 out('$returnType get $fieldName => $_throwDeprecated;'); 857 out('$returnType get $fieldName => $_throwDeprecated;');
820 } else { 858 } else {
821 out('$returnType get $fieldName {'); 859 out('$returnType get $fieldName {');
822 indent(() { 860 indent(() {
823 String readExpr = '$readCode.vTableGet(_bc, _bcOffset, $index, $def) '; 861 String readExpr =
862 '$readCode.vTableGet(_bc, _bcOffset, $index, $def)';
824 out('_$fieldName ??= $readExpr;'); 863 out('_$fieldName ??= $readExpr;');
825 out('return _$fieldName;'); 864 out('return _$fieldName;');
826 }); 865 });
827 out('}'); 866 out('}');
828 } 867 }
829 } 868 }
830 }); 869 });
831 out('}'); 870 out('}');
832 } 871 }
833 872
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
911 String name = cls.name; 950 String name = cls.name;
912 out('${idlPrefix(name)} read$name(List<int> buffer) {'); 951 out('${idlPrefix(name)} read$name(List<int> buffer) {');
913 indent(() { 952 indent(() {
914 out('fb.BufferContext rootRef = new fb.BufferContext.fromBytes(buffer);'); 953 out('fb.BufferContext rootRef = new fb.BufferContext.fromBytes(buffer);');
915 out('return const _${name}Reader().read(rootRef, 0);'); 954 out('return const _${name}Reader().read(rootRef, 0);');
916 }); 955 });
917 out('}'); 956 out('}');
918 } 957 }
919 958
920 /** 959 /**
960 * Generate a call to the appropriate method of [ApiSignature] for the type
961 * [typeName], using the data named by [ref]. If [couldBeNull] is `true`,
962 * generate code to handle the possibility that [ref] is `null` (substituting
963 * in the appropriate default value).
964 */
965 void _generateSignatureCall(String typeName, String ref, bool couldBeNull) {
966 if (_idl.enums.containsKey(typeName)) {
967 if (couldBeNull) {
968 out('signature.addInt($ref == null ? 0 : $ref.index);');
969 } else {
970 out('signature.addInt($ref.index);');
971 }
972 } else if (_idl.classes.containsKey(typeName)) {
973 if (couldBeNull) {
974 out('signature.addBool($ref != null);');
975 }
976 out('$ref?.collectApiSignature(signature);');
977 } else {
978 switch (typeName) {
979 case 'String':
980 if (couldBeNull) {
981 ref += " ?? ''";
982 }
983 out("signature.addString($ref);");
984 break;
985 case 'int':
986 if (couldBeNull) {
987 ref += ' ?? 0';
988 }
989 out('signature.addInt($ref);');
990 break;
991 case 'bool':
992 if (couldBeNull) {
993 ref += ' == true';
994 }
995 out('signature.addBool($ref);');
996 break;
997 case 'double':
998 if (couldBeNull) {
999 ref += ' ?? 0.0';
1000 }
1001 out('signature.addDouble($ref);');
1002 break;
1003 default:
1004 throw "Don't know how to generate signature call for $typeName";
1005 }
1006 }
1007 }
1008
1009 /**
921 * Return the documentation text of the given [node], or `null` if the [node] 1010 * Return the documentation text of the given [node], or `null` if the [node]
922 * does not have a comment. Each line is `\n` separated. 1011 * does not have a comment. Each line is `\n` separated.
923 */ 1012 */
924 String _getNodeDoc(LineInfo lineInfo, AnnotatedNode node) { 1013 String _getNodeDoc(LineInfo lineInfo, AnnotatedNode node) {
925 Comment comment = node.documentationComment; 1014 Comment comment = node.documentationComment;
926 if (comment != null && 1015 if (comment != null &&
927 comment.isDocumentation && 1016 comment.isDocumentation &&
928 comment.tokens.length == 1 && 1017 comment.tokens.length == 1 &&
929 comment.tokens.first.type == TokenType.MULTI_LINE_COMMENT) { 1018 comment.tokens.first.type == TokenType.MULTI_LINE_COMMENT) {
930 Token token = comment.tokens.first; 1019 Token token = comment.tokens.first;
931 int column = lineInfo.getLocation(token.offset).columnNumber; 1020 int column = lineInfo.getLocation(token.offset).columnNumber;
932 String indent = ' ' * (column - 1); 1021 String indent = ' ' * (column - 1);
933 return token.lexeme.split('\n').map((String line) { 1022 return token.lexeme.split('\n').map((String line) {
934 if (line.startsWith(indent)) { 1023 if (line.startsWith(indent)) {
935 line = line.substring(indent.length); 1024 line = line.substring(indent.length);
936 } 1025 }
937 return line; 1026 return line;
938 }).join('\n'); 1027 }).join('\n');
939 } 1028 }
940 return null; 1029 return null;
941 } 1030 }
942 } 1031 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698